There is some code:

A a1; A a2; A a3; std::vector<A> a; std::cout << "Push back a1" << std::endl; a.push_back(a1); std::cout << "Push back a2" << std::endl; a.push_back(a2); std::cout << "Push back a3" << std::endl; a.push_back(a3); 

And of course class A.

 class A { static int ACount; private: public: A() { std::cout << "Constructor called. Objects = " << ++ACount << std::endl; } ~A() { std::cout << "Destructor called. Objects = " << --ACount << std::endl; } A(const A &a) { std::cout << "Copy Constructor called. Objects = " << ++ACount << std::endl; } }; int A::ACount; 

The output from the console is:

 Constructor called. Objects = 1 Constructor called. Objects = 2 Constructor called. Objects = 3 Push back a1; Copy constructor called. Objects = 4 Push back a2; Copy constructor called. Objects = 5 Copy constructor called. Objects = 6 Destructor called. Objects = 5 Push back a3; Copy constructor called. Objects = 6 Copy constructor called. Objects = 7 Copy constructor called. Objects = 8 Destructor called. Objects = 7 Destructor called. Objects = 6 Destructor called. Objects = 5 Destructor called. Objects = 4 Destructor called. Objects = 3 Destructor called. Objects = 2 Destructor called. Objects = 1 Destructor called. Objects = 0 

I can not understand why when I call the Push_back method (a2), the copy constructor is called 2 times, and when Push_back (a3), 3 times. I am trying to create a kind of graph of the engine and the creation / destruction of such a number of objects is very harmful to me. How to be? Or is it worth looking for some other container?

  • Add after std::vector<A> a; call a.reserve(10); and see the results ... - Harry

2 answers 2

There is a relocation of the vector with each push_back . You see calls to copy constructors to copy elements from the old place to a new one, and then to destroy the elements in the old place.

Make a preliminary

 a.reserve(100); 

and the "extra" copying and destruction will disappear.

  • Thank you very much. And another question: When the vector 'std :: vector <A> a' is created, does it reserve memory for one element or not? That is, the relocation occurs every time you call push_back () or only the second time? - Sergey Patient
  • @ Sergey Patient: This is determined by the implementation. The language standard does not impose any requirements on the initial capacity of the vector. Request capacity() from your vector immediately after the constructor and see what it returns. Most likely 0 . - AnT

I can not understand why when I call the Push_back method (a2), the copy constructor is called 2 times, and when Push_back (a3), as many as 3 times

When the size of a vector is increased, a new vector is created, all the elements of the old and the push element are copied into it, and then all the elements of the old are destroyed.

This is due to the fact that the vector ensures consistent continuous storage of elements in memory.

How to be?

You can reserve a sufficient number of items in advance.

Or is it worth looking for some other container?

It depends on what you need from the container. What kind of operations you will do with it.

For a vector, an arbitrary element is searched for a constant time, and adding a new one (unless you specifically reserve space) for O (N). You can also pass a vector to a function as a pointer to a C-like array.

The list for the constant time is a search, add and delete the first and last element, and an arbitrary element - for O (N).

  • 3
    A new vector is not created, but a new piece of memory is allocated. And not 'when increasing the size', but 'when increasing the capacity' (capacity). - HolyBlackCat