vector<X> v;
X x;
v.push_back(x); v.push_back(x); v.push_back(x);
Why this code calls the copy constructor of a class X 6 times? (using g++ 4.7.2 STL)
Please, I’d like to know it precisely what happens under hood with this particular STL.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you insert
xwithpush_back(), the memory is reallocated eventually to make room for the new element. The already inserted members must then be copied around using the copy constructorX(const X&).If you insert
reallocation is prevented for at least the first three
push_back()s and, as a result, there will be only three calls toX(const X&)