vector::push_back( someClass ) odd behaviour

I am generating a vector of a class. Somehow when I am using 'push_back', the length of the vector increases exponentially instead of linearly.

Certainly it has something to do with constructors and it is probably very basic but I am relatively new to C++ and couldn't solve the problem.

I would really appreciate it if somebody could help me with this.

following code produces this output:

capacity: 0
capacity: 1
capacity: 2
capacity: 4
capacity: 4
capacity: 8
capacity: 8
capacity: 8
capacity: 8
capacity: 16


code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>

using namespace std;

class class1
{
	public:
	int a;
	float b;
};

class class2
{
	public:
	vector < class1 > c;
};

int main(int argc,char** argv)
{
	int f;
	class2 sok1;
	class1 buff;
	
	buff.a=10;
	buff.b=10.22;
	
	for( f=0;f<10;f++)
	{
		cout << "capacity: " << sok1.c.capacity() << "\n";
		sok1.c.push_back(buff);
	}

}
The capacity is how much size the vector has reserved. The general standard is for the capacity to double in size every time it is required to extend.

To see how many items you have in a vector, you use the .size() member.
thanks for the quick answer, looks like a really dumb question now :)
Actually, I think the standard is to increase the size of the vector by a factor less than or equal to the golden ratio.
gcc, unfortunately, doubles it, which can lead to memory fragmentation.
This is why you should use the reserve member function before pushing data into an empty vector, if you can. If you have some reasonable idea as to how large the vector might grow then you should reserve some memory for elements. This way the size can grow or shrink until the capacity is reached.
Stroustrup wrote:
People sometimes worry about the cost of std::vector growing incrementally. I used to worry about that and used reserve() to optimize the growth. After measuring my code and repeatedly having trouble finding the performance benefits of reserve() in real programs, I stopped using it except where it is needed to avoid iterator invalidation (a rare case in my code). Again: measure before you optimize.


From http://www2.research.att.com/~bs/bs_faq2.html#slow-containers
An even better idea is to use the right container for the job. These questions usually help in the simple case:

Do the elements have to be contiguous in memory?
Do you need random access to the elements?

A vector stores its elements contiguously, which is why it resizes when enough elements are inserted, and provides random access.

A deque provides random access, but does not store elements contiguously.

A list does not provide either random access nor does it store elements contiguously.

Those descriptions should be helpful but realize that they are certainly not comprehensive. A good book on the STL is a valuable investment for C++ developers.
Topic archived. No new replies allowed.