473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Operation of STL vector

Hello,

For my application, I have a vector containing data that I need,
vector<NODEnode_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

May 11 '07 #1
4 1866
the old pointer is invalid after you adding a new item to the vector.

so i suggest you use std::list.

"Tony" <ki**@mit.edu>
??????:11**********************@n59g2000hsh.google groups.com...
Hello,

For my application, I have a vector containing data that I need,
vector<NODEnode_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

May 11 '07 #2
On 2007-05-11 17:53, Tony wrote:
Hello,

For my application, I have a vector containing data that I need,
vector<NODEnode_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.

--
Erik Wikström
May 11 '07 #3
Tony <ki**@mit.eduwrote:
For my application, I have a vector containing data that I need,
vector<NODEnode_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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 11 '07 #4
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!)

May 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gilles Arnaud | last post by:
Hello, I've got a nasty bug and no idea to deal with : here is the method : <method> def denormer(self, var) : " denorme un vecteur d'entree " try: #a = map(self.decerner, self.param, var)
12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
2
by: marko.suonpera | last post by:
I'm using the Newmat library where element access in ColumnVector type is defined as follows: typedef double Real; Real& ColumnVector::operator()(int m) { REPORT if (m<=0 || mnrows)...
8
by: Mugunth | last post by:
What is the most effective way of implementing an AND operation on a two-d array. I've an array like, 1 3 5 6 7 2 4 5 6 1 8 6 4 9 2 .. .. ..
1
by: subramanian100in | last post by:
Suppose I have vector<intvi; deque<intdi; list<intli; Suppose all of these containers have some elements. Suppose 'v_iter' is an iterator pointing to some element in 'vi'. Suppose 'v_beg'...
3
by: subramanian100in | last post by:
In the following program, I have used operator==() as member function for learning purpose only. consider the following program: #include <cstdlib> #include <iostream> #include <vector> ...
2
ashitpro
by: ashitpro | last post by:
Hello guys, I got two vectors, such as vector<string> A vector<string> B Both vectors are having more than 100 strings in it. I want to perform "minus" operation on it... i.e (A - B)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.