+ 2

vector and copy constructor c++

#include <iostream> #include <vector> class A{ public: A(){std::cout << "dc" << std::endl;} A(const A& x){std::cout << "cc" << std::endl;} }; int main() { std::vector<A> y; y.push_back(A()); y.push_back(A()); return 0; }; the result: dc cc dc cc cc calling default constructor twice probably still made sense for me cause i created 2 object and push it to the vector.. but can someone explain to me why vector call copy constructor thrice?

12th Apr 2020, 1:30 PM
durian
durian - avatar
15 ответов
+ 3
I got the same as you, Lily. I expected two times each. 😅
12th Apr 2020, 3:14 PM
HonFu
HonFu - avatar
+ 2
Lily Mea That's doesn't make sense. For me there should be 3 dc and 2 cc
12th Apr 2020, 2:12 PM
C ++
C ++ - avatar
+ 2
Why are there no more copies for the last entry? And isn't that inefficient to copy all the time? (Still don't really get it.)
12th Apr 2020, 3:31 PM
HonFu
HonFu - avatar
+ 2
Just a bit of fact about how std::vector allocates capacity. The additional space reserved grows with number of items added to the vector. It is not necessarily be one extra capacity for every additional item. https://code.sololearn.com/c7ailW9I1ptT/?ref=app
13th Apr 2020, 2:23 AM
Ipang
+ 1
https://code.sololearn.com/cDz1exwC4D5w/?ref=app Vector in c++ increase its size by 1 every time we add new element in it. Internally it stores these in array. And every time it increase its size it push_back the given value then copy all the values already in the array and then delete the old array. Therefore it calls cc multiple times. Check this code for detail. x in it is used for demonstration. Lily Mea I hope you will understand.
12th Apr 2020, 3:15 PM
C ++
C ++ - avatar
+ 1
12th Apr 2020, 3:16 PM
C ++
C ++ - avatar
+ 1
The last item is already in the newly created array. Therefore, there is no need to copy it again. It has the overhead of copying each time but as a developer, we have to select an appropriate data structure. It is neglectable for small vectors. But for a large quantity of data and if we don't know the size and there might be large amount of items we should think about any other data structure.
12th Apr 2020, 4:05 PM
C ++
C ++ - avatar
+ 1
i have a question, for example my vector have size of 2 and its already full,when i try to push back a new object,it will create 2 more room, and the first 2 objects will be copied to the new room before the 3rd is assigned to it own new object?
12th Apr 2020, 4:15 PM
durian
durian - avatar
+ 1
If the vector already has two items in it. Actually it has an array of size two which stores those two items in it. Suppose this array is A. Now when we push 3rd item to vector, it creates a new array ( Suppose this array B) and stores this 3rd item in the array B at its position. And then copy items 1 and 2 from array A to the array B and delete array A. Every time we push a new item it does the same process. create new array copy values from old to the new array and delete the old array.
12th Apr 2020, 4:25 PM
C ++
C ++ - avatar
+ 1
Thanks for more explanation. Ipang
13th Apr 2020, 3:15 AM
C ++
C ++ - avatar
0
but that is the output
12th Apr 2020, 2:15 PM
durian
durian - avatar
0
I'll check it. Wait
12th Apr 2020, 2:16 PM
C ++
C ++ - avatar
0
i kinda get it now
13th Apr 2020, 1:49 AM
durian
durian - avatar
0
i find it more clear after printing the address...somehow kinda get how it works....thanks
13th Apr 2020, 1:50 AM
durian
durian - avatar