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

  • Thread starter Steven T. Hatton
  • Start date
S

Steven T. Hatton

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/isocpp/lib-containers.html#lib.vector
// lib.vector.capacity capacity:
size_type size() const;
size_type max_size() const;
void resize(size_type sz, T c = T());
size_type capacity() const;
bool empty() const;
void reserve(size_type n);

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

Andrey Tarasevich

Steven said:
...
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.
 
L

Leor Zolman

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/isocpp/lib-containers.html#lib.vector
// lib.vector.capacity capacity:
size_type size() const;
size_type max_size() const;
void resize(size_type sz, T c = T());
size_type capacity() const;
bool empty() const;
void reserve(size_type 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 "invalidates". You wish to avoid
anything that invalidates references, pointers and iterators into the
vector. If you search the Standard for "invalidates", 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
 
I

Ioannis Vranos

Steven T. Hatton said:
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/isocpp/lib-containers.html#lib.vector
// lib.vector.capacity capacity:
size_type size() const;
size_type max_size() const;
void resize(size_type sz, T c = T());
size_type capacity() const;
bool empty() const;
void reserve(size_type 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>::iterator p=a.begin()+3;

*p=7;

a.push_back(4);

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






Regards,

Ioannis Vranos
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top