Operation of STL vector

T

Tony

Hello,

For my application, I have a vector containing data that I need,
vector<NODE> node_data; and I have a pointer of vectors which are
sorted by some criterion as vector<NODE*> np. I have planned the data
management as:

Whenever I want to add a new node, I push_back onto the end of
node_data and create a corresponding reference in np. However, I find
that only the last-added pointer in np correctly points to the right
location in memory of the NODE elements. My suspicion is that when
vector increases its size in memory, it completely reallocates a
larger space in memory such that the old references are no longer
valid. Is this true? i.e. when increasing the size of a vector, a
completely new structure is created in memory rather than just
"tacking on" an additional element at the end?

If I wanted to be able to leave the old information alone, and
actually just "tack on" something at the end, would the List be a
better choice?



Finally, could I get some suggestions for a "casual" book on
algorithms (as far as casual they can be!) for a hobbyist programmer?


Thank you very much
 
P

Paolo Maldini

the old pointer is invalid after you adding a new item to the vector.

so i suggest you use std::list.
 
M

Marcus Kwok

Tony said:
For my application, I have a vector containing data that I need,
vector<NODE> node_data; and I have a pointer of vectors which are
sorted by some criterion as vector<NODE*> np. I have planned the data
management as:

Whenever I want to add a new node, I push_back onto the end of
node_data and create a corresponding reference in np. However, I find
that only the last-added pointer in np correctly points to the right
location in memory of the NODE elements. My suspicion is that when
vector increases its size in memory, it completely reallocates a
larger space in memory such that the old references are no longer
valid. Is this true? i.e. when increasing the size of a vector, a
completely new structure is created in memory rather than just
"tacking on" an additional element at the end?

It depends on the capacity() of the vector. If the new element can fit,
then will just add it to the end. However, if adding the new element
would cause the vector to exceed its capacity(), then a reallocation
will occur and the elements will be copied into the larger reallocated
space.

If you know how many elements will be in the vector, you can reduce or
eliminate the reallocations by reserve()-ing space for the appropriate
number of elements.
If I wanted to be able to leave the old information alone, and
actually just "tack on" something at the end, would the List be a
better choice?

std::list is one option: AFAIR the only list operation that invalidates
iterators (or pointers) to elements is delete, and that only invalidates
the element pointed at. std::deque is another option worth looking
into, as it is sort of a cross between vector and list.
Finally, could I get some suggestions for a "casual" book on
algorithms (as far as casual they can be!) for a hobbyist programmer?

Well, it's pretty academic (so probably not as casual as you want) but I
used CLRS (_Introduction to Algorithms_ by Cormen, Leiserson, Rivest,
and Stein: http://en.wikipedia.org/wiki/Introduction_to_Algorithms) in
my algorithms class.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hello,

For my application, I have a vector containing data that I need,
vector<NODE> node_data; and I have a pointer of vectors which are
sorted by some criterion as vector<NODE*> np. I have planned the data
management as:

Whenever I want to add a new node, I push_back onto the end of
node_data and create a corresponding reference in np. However, I find
that only the last-added pointer in np correctly points to the right
location in memory of the NODE elements. My suspicion is that when
vector increases its size in memory, it completely reallocates a
larger space in memory such that the old references are no longer
valid. Is this true? i.e. when increasing the size of a vector, a
completely new structure is created in memory rather than just
"tacking on" an additional element at the end?

Not every time you add a new element but yes it happens.
If I wanted to be able to leave the old information alone, and
actually just "tack on" something at the end, would the List be a
better choice?

A list would work. However if you know from the beginning the number of
NODEs that will be pushed onto the vector you can use the reserve()
method to make sure that the vector can contain all the NODEs, in which
case it will not reallocate (as long as you don't add more NODEs than
your reserved.
Finally, could I get some suggestions for a "casual" book on
algorithms (as far as casual they can be!) for a hobbyist programmer?

Do you want an algorithm book, describing different types of algorithms
and analyses them, or a book describing how to implement different
algorithms and data structures in a specific language, or do you want a
book about how to effectively use the data structures and algorithms
that comes with standard C++?

You'll probably get the most benefit (at least in the short term) with
the last kind of book and a highly recommended one is The C++ Standard
Library: A Tutorial and Reference by Nicolai M. Josuttis. I don't know
of any good books of the second kind (not saying that there are none,
just that I don't know of any) and the first kind can be a bit much for
a hobbyist.
 
T

Tony

Thanks everyone! For now, the list met my needs.

(As it happens, I have the opportunity to take an algorithms course at
my school from Rivest!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top