Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

std::vector ?

What the difference between these two syntaxs ?

std::vector<int> vec(10);

std::vector<int> vec;
vec.reserve(10);

because when I used vec.size() & vec.capaciry(), the result is not the same.
For vec.size(), the result is "0" ,whilst "10" for vec.reserve();
[302 byte] By [Butterfly] at [2007-11-11 8:03:15]
# 1 Re: std::vector ?
vec(n) is a constructor which puts n elements of the default value into your newly constructed vector. Your size will be 10. Your capacity will follow normal stl vector rules.

vec.reserve(n) increases the capacity to n if it is not already at n. It reserves memory locations, but does not put values into those locations. It does not increase size. So if there are no elements stored in your vector with capacity 10, the size is still 0.
nspils at 2007-11-11 21:01:44 >
# 2 Re: std::vector ?
Summarizing nspil's reply:
std::vector<int> vec(10);

causes the size to be 10

vec.reserve(10);
causes capacity to be 10, while not altering the size.
Danny at 2007-11-11 21:02:45 >
# 3 Re: std::vector ?
Thanks all,

but I have something confusing me.

vector<int> v;

v.reserve( 2 );

v[0] = 1;
v[1] = 2;
for( vector<int>::const_iterator i = v.begin();
i != v.end(); i++ )
{
cout << *i << endl;
}

why the output is not "1" , "2" ?
Butterfly at 2007-11-11 21:03:44 >
# 4 Re: std::vector ?
What IS your output?
nspils at 2007-11-11 21:04:39 >
# 5 Re: std::vector ?
no output.
it does not go through for loop.
Butterfly at 2007-11-11 21:05:49 >
# 6 Re: std::vector ?
why do you expect any outgput? the loop should be emptgy because tghe vector's size is 0. You need to insertg something to it using push_back before you execute this loop. capacity us not the same thing as size!
Danny at 2007-11-11 21:06:48 >
# 7 Re: std::vector ?
Thanks Danny, that's my point.
So what's this code means ?

v[0] = 1;
v[1] = 2;

Does it not assign the values to vector ?
Butterfly at 2007-11-11 21:07:46 >
# 8 Re: std::vector ?
No, this code causes a runtime crash (at best!). You're actually causing a potential buffer overflow here, assigning values to memory slots that might not exist. When using vectors, you need to call vec.push_back (5) etc to insert a value to the next element *and* thereby ensure that the vector allocates the storage for that element. v[0]=1; works only if v[0] has already been allocated using push_back etc.
Danny at 2007-11-11 21:08:49 >
# 9 Re: std::vector ?
What the others are trying to say is, when you call resize you're changing the size of the vector allowing you to access elemnts in it (v[3] = 23; etc...).

However, calling reserve does NOT change its size. It's simply a way to preallocate memory for it so when resizing at a later time things will (might) go faster.

Calling v[3] = 23; on a vector with a size less than 4 will NOT resize it.
Magos at 2007-11-11 21:09:53 >
# 10 Re: std::vector ?
You're actually causing a potential buffer overflow here, assigning values to memory slots that might not exist

Danny, as I understand, when we call reserve(), it will allocate internal memory. That's why I confuse.
Butterfly at 2007-11-11 21:10:48 >