473,779 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1882
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.g ooglegroups.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.eduwr ote:
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
6597
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
10953
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 the end - obviously its O(n). However, keeping track of inserts and deletes isn't that tough, so its conceivable (to me!) that size() could be O(1). For my application, the lists can be quite long (millions of elements), so the answer greatly...
9
3208
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
8
5115
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
2
3169
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) Throw(IndexException(m,*this)); return store;
8
1597
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
2540
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' and 'v_end' are valid iterators pointing to some
3
1609
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> using namespace std;
2
2130
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) What should be the efficient method for this. I can change my data structures i.e. vector to Set or may be map....Data structure is not a problem..
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.