Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2000-05-08
5
Medium Priority
?
235 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 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

926 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