473,791 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I trust a vector to stay put if I don't add to it?

My understanding of the containers in the Standard Library is that they can
move out from under pointers and references if they are resized.
Stroustrup suggests I can reserve the capacity I will need, and thus avoid
having the vector (or other container) realocated with a different address
range. I have the sense this is not explicitly specified in the Standard.
I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
an oder version draft of the Standard:

http://www.kuzbass.ru:8086/docs/isoc...tml#lib.vector
// lib.vector.capa city capacity:
size_type size() const;
size_type max_size() const;
void resize(size_typ e sz, T c = T());
size_type capacity() const;
bool empty() const;
void reserve(size_ty pe n);

Is it reasonable for me to assume my vector /will/ stay put if I don't force
a reallocation by adding to it?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
3 1344
Steven T. Hatton wrote:
...
Is it reasonable for me to assume my vector /will/ stay put if I don't force
a reallocation by adding to it?


Yes. It is specified in the standard.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2
On Thu, 06 May 2004 20:58:29 -0400, "Steven T. Hatton"
<su******@setid ava.kushan.aa> wrote:
My understanding of the containers in the Standard Library is that they can
move out from under pointers and references if they are resized.
Stroustrup suggests I can reserve the capacity I will need, and thus avoid
having the vector (or other container) realocated with a different address
range. I have the sense this is not explicitly specified in the Standard.
I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
an oder version draft of the Standard:

http://www.kuzbass.ru:8086/docs/isoc...tml#lib.vector
// lib.vector.capa city capacity:
size_type size() const;
size_type max_size() const;
void resize(size_typ e sz, T c = T());
size_type capacity() const;
bool empty() const;
void reserve(size_ty pe n);

Is it reasonable for me to assume my vector /will/ stay put if I don't force
a reallocation by adding to it?


The word you're interested here is "invalidate s". You wish to avoid
anything that invalidates references, pointers and iterators into the
vector. If you search the Standard for "invalidate s", you'll find
23.2.4.2/5, which answers your direct question, and also a bit later the
section on vector::erase() explains what gets invalidated when you use
that.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:9c******** ************@sp eakeasy.net...

My understanding of the containers in the Standard Library is that they can move out from under pointers and references if they are resized.
Stroustrup suggests I can reserve the capacity I will need, and thus avoid
having the vector (or other container) realocated with a different address
range. I have the sense this is not explicitly specified in the Standard.
I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
an oder version draft of the Standard:

http://www.kuzbass.ru:8086/docs/isoc...tml#lib.vector
// lib.vector.capa city capacity:
size_type size() const;
size_type max_size() const;
void resize(size_typ e sz, T c = T());
size_type capacity() const;
bool empty() const;
void reserve(size_ty pe n);

Is it reasonable for me to assume my vector /will/ stay put if I don't force a reallocation by adding to it?

Yes. However you can get the begin or end (one past the end element) of the
vector whenever you want, so you can do for example:
#include <vector>
int main()
{
using std::vector;

vector<int>a(10 );

vector<int>::it erator p=a.begin()+3;

*p=7;

a.push_back(4);

// p continues to point at a[3]
p=a.begin()+3;
}


Regards,

Ioannis Vranos

Jul 22 '05 #4

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

Similar topics

9
3209
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 **********
27
5982
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
11
8882
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse perfomance than if I first define the vector of a given size, then write to the elements with myvec = However, I'm currently thinking that it isn't feasible to obtain the vector size, so really need to resize the vector dynamically as I go. Is...
13
2154
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================ class Buyer{ void start(void); friend void Buyer_run(Buyer *buyer);
11
2747
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
17
3361
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
3
1847
by: nick | last post by:
Okay guys, I got my vector to work but how do I do the following: 1. I create a bank account object. 2. I place that bank account object in the vector 3. I have a loop that repeats steps 1 and 2 My question is.....how do I get the vector to have objects of the same type, but with different names?
4
2389
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual function to change it to a std::string (it's a library function) so I was thinking, well, I could use a std::vector<charinstead of a char array, but would that actually gain me anything at all? I mean, what's the difference between calling it like the...
15
5872
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read its coontents into a vector of strings, storinig each line as separate
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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
10427
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...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9029
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
6776
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
5431
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...
1
4110
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
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.