Solved

How can I delat a <list> versus <vector> decision?

Posted on 2000-05-08
5
196 Views
Last Modified: 2010-04-02
I want to put several items (which I will call contigs) into a container.  I haven't decided yet whether to use <list>  or <vector> or conceivably something else.

Now I have another structure that needs to point
to various contigs (elements in the above container).

If I use a<vector> to hold the contigs, I would
use an index into that vector.  If I use a <list> to
hold the contigs, I would use a pointer or iterator.

At first I thought that an iterator was the common
denominator.  But, iterators become invalid if the
vector is lengthened (I think).

Thoughts?

Ken
0
Comment
Question by:klopter
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2789144
You can't really be certain about the contents of -any- iterator even a pointer, what do you do if the object that you have a reference to is deleted from the container?

The only completely safe way is to query for items in a container as they are required.  

However, if you can control deletion of objects from a container,  then you are correct, a list iterator is never invalidated by -other- deletions/insertions/reorganisations.  Also pointers and references to objects in the list stay valid.

You could use a vector in a similar way, but you would have to store a vector of pointers to objects and then use the pointers as the references to the objects.  You cannot reliably use the index into a vector as a reference, however.
0
 

Author Comment

by:klopter
ID: 2789195
I can't query for the items in the container without changing an O(n)
algorithnm into an O(n^2) algorithm.
With n in the millions, O(n^2) is not feasible.

I realize that as I change the contents of the container I have to update my links to the that container.  For example, if I delete an entry in the container, I have to invalidate all pointers to that entry.  In order to do this I need a pointer to those pointers, i.e. they have to be doubly linked.  

But, I don't want to have to update all those pointers as a result of an internal vector resize.  

Ken
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2790160
erasing from or inserting into list<> does *not* invalidate existing iterators (except off course iterators that happen to be pointing at an erased element). But you knew that.

You would only need to keep iterators if you plan to use them as such. If you only need to reference the objects a smart-pointer of some some sort might be usable.
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 100 total points
ID: 2791618
I am not sure what the issue is now, with a list, as I stated before, all pointers/references/iterators to the elements stay valid (unless the pointed to item is deleted).

To use a vector in a similar way, the only safe thing to do is to store the contained objects by pointer and then hold on to the pointers.

i.e.:

list<MyClass> myList;

MyClass* mc = &myList.front();  // This pointer is always valid
list<MyClass>::iterator i = myList.begin();  // This iterator stays valid

or with a vector:

vector<MyClass*> myVector;
MyClass* mc = myVector[0]; // This pointer will stay valid

You could potentially store an index into a vector, if and only if you can be certain that all additions to the vector are at the end (i.e. only push_back is used).

So, what is the question now?
0
 

Author Comment

by:klopter
ID: 2792539
This is the answer to my question:

To use a vector in a similar way, the only safe thing to do is to store the contained objects by pointer and then hold on to the pointers.

If I store the objects by pointer, I can switch from <list> to <vector> or vice versa.  

I am always reluctant to throw in a level of indirection, but I am now convinced that if I want that generality, that is my only option.

Ken
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now