-
April 10th, 2006, 09:50 AM
#1
STL size, capacity, constructor
Code:
vector <int> v1 (20);
vector <int> v2;
v2.reserve(20);
Later in the code when I do:
Code:
int c1 = v1.capacity();
int c2 = v2.capacity();
int s1 = v1.size();
int s2 = v2.size()
The capacity() call returns correct value but s1 and s2 are diferent with s1 being 20 but s2 is 0.
I wish to use only [] operator to handle the vectors, does that mean I should always use constructor to set the size of vector?
Good Answers, Good Points.
-
April 10th, 2006, 10:01 AM
#2
Re: STL size, capacity, constructor
Originally Posted by retry
The capacity() call returns correct value but s1 and s2 are diferent with s1 being 20 but s2 is 0.
All of those values are correct, not just the capacity() values. Do you know the difference between size(), capacity(), and reserve()?
I wish to use only [] operator to handle the vectors, does that mean I should always use constructor to set the size of vector?
You have resize() to size a vector after has been constructed.
Regards,
Paul McKenzie
-
April 10th, 2006, 10:03 AM
#3
Re: STL size, capacity, constructor
IIRC, reserve just makes sure you have enough space for pushing that many elements into the vector. In other words, it doesn't change the vector's size, just the amount of reserved memory for storage.
You probably want to use 'resize' not 'reserve'.
Viggy
-
April 10th, 2006, 10:04 AM
#4
Re: STL size, capacity, constructor
Darn you Paul!!!
Viggy
-
April 10th, 2006, 10:04 AM
#5
Re: STL size, capacity, constructor
Originally Posted by retry
The capacity() call returns correct value but s1 and s2 are diferent with s1 being 20 but s2 is 0.
The behaviour you saw is correct, and by design.
Capacity is a term that indicates the number of elements that can be inserted into the vector without it needing to allocate for more memory, and copy, etc. std::vector::reserve sets the capacity.
Size is a term that indicates the number of elements constructed within the vector. Hence, capacity is always greater than or equal to size.
When you constructed the vector v1 with 20 as a parameter, you actually specified the size of the vector. It's capacity could be as much or greater - depending on the implementation.
Originally Posted by retry
I wish to use only [] operator to handle the vectors, does that mean I should always use constructor to set the size of vector?
You can do that.
If you set the capacity, you still do need to push_back elements into the vector. However, if you specify a number in the constructor or use std::vector::resize, you may simply index it (within it's bounds) using operator [], as this will simply copy values into the existing objects.
/ Edit: Paul beat me...
Last edited by Siddhartha; April 10th, 2006 at 10:08 AM.
-
April 10th, 2006, 12:47 PM
#6
Re: STL size, capacity, constructor
See this article on std::vector.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|