What is the difference between copy and assigning a vector ? line 2 and 4 .
1 vector<int> V1(5);
2 vector<int> V3(V1);
3 vector<int> V4(V1.size());
4 V4 = V1 ;
What is the difference between copy and assigning a vector ? line 2 and 4 .
1 vector<int> V1(5);
2 vector<int> V3(V1);
3 vector<int> V4(V1.size());
4 V4 = V1 ;
Here is a doxygen extract from my stl implementation for operator=:
/* All the elements of @a x are copied, but any extra memory in
* @a x (for fast expansion) will not be copied. Unlike the
* copy constructor, the allocator object is not copied.
*/
As you can see there is a difference if you use custom allocators, but in other cases the result is the same.
Line 2 uses a copy constructor. Line 4 uses a copy assignment. Both create copies; the first creates a new object and the second overwrites an existing object.