La mejor estructura/contenedor de datos en C++ para inserción y eliminación

I am looking for the best data structure for C++ in which insertion and deletion can take place very efficiently and fast.

Traversal should also be very easy for this data structure. Which one should i go with? What about SET in C++??

preguntado el 04 de julio de 12 a las 10:07

You need to provide more information. How exactly do you want to use it? -

Ite depends on so many factors, including, how many items your going to be storing. -

So many other factors also play an important does insertion and deletion are at the end/beginning or at random places? how important is the lookup performance? Are duplicates allowed? -

I have some cells and leaves. When a cell is split, i want to store in as a leaf in the other cell and all that stuff.. Basically the insertion may not be sequential. They can be random. I just want a container which has the leaves....order does not matter.....i want something like an array but more efficient in searching and all. -

Are you sure about this? 8^20 is 2^60. Since you can't have duplicates, you need at least 60 bits per item. That's 8.65 exabytes if you store them sequentially - without a fancy structure. You won't fit that in a regular computer any time soon. -

4 Respuestas

A linked -- provides efficient insertion and deletion of arbitrary elements. Deletion here is deletion by iterator, not by value. Traversal is quite fast.

A quitar provides efficient insertion and deletion only at the ends, but those are faster than for a linked list, and traversal is faster as well.

A planificadas only makes sense if you want to find elements by their value, e.g. to remove them. Otherwise the overhead of checking for duplicate as well as that of keeping things sorted will be wasted.

Respondido 04 Jul 12, 11:07

It depends on what you want to put into this data structure. If the items are unordered or you care about their order, list<> could be used. If you want them in a sorted order, set<> or multiset<> (the later allows multiple identical elements) could be an alternative.

list<> is typically a double-linked list, so insertion and deletion can be done in constant time, provided you know the position. traversal over all elements is also fast, but accessing a specified element (either by value or by position) could become slow.

set<> and its family are typically binary trees, so insertion, deletion and searching for elements are mostly in logarithmic time (when you know where to insert/delete, it's constant time). Traversal over all elements is also fast.

(Note: boost and C++11 both have data structures based on hash-tables, which could also be an option)

Respondido 04 Jul 12, 11:07

When you split a leaf you roughly know where to insert the new one, so it'll be constant time. - Peter Wood

I would say a linked list depending on whether or not you're deletions are specific and often. Iterator about it.

Respondido 04 Jul 12, 11:07

It occurs to me, that you need a árbol.

I'm not sure about the exact structure (since you didnt provide in-detail info), but if you can put your data into a árbol binario, you can achieve decent speed at searching, deleting and inserting elements ( O (logn) promedio y O (n) worst case).

Note that I'm talking about the data structure here, you can implement it in different ways.

Respondido 04 Jul 12, 11:07

A set is most often implemented using a árbol rojo-negro, which is a form of binary tree and will guarantee O(log n) for deletion and insertion. Re-implementing that tree will yield little value. - MvG

@MvG, I didn't say he needs to re-mplement it, I just named the data structure which he can use differently, depending on his data (it's still not quite clear how the data looks, e.g. does he have key-value pairs or not).If a set or multiset suits him well, it's just fone. - SingerOfTheFall

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.