Home

C++ vector copy assignment

The copy assignment (1) copies all the elements from x into the container (with x preserving its contents). The move assignment (2) moves the elements of x into the container (x is left in an unspecified but valid state). The initializer list assignment (3) copies the elements of il into the container.

The container preserves its current If some userdefined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default. (since C11) The implicitlydeclared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in dynamic Prior to C11 your code would not have been possible: the vector element type had to be CopyAssignable, meaning copyconstructible and assignable.

Note that if the type does not meet these requirements, the code is illformed with no diagnostic required; this gives compilers latitude to either accept or reject it. I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy).

Could you hint to an explanation? Regards Ways to copy a vector in C. In case of arrays, there is no much choice to copy an array into other, other than iterative method i.

e running a loop to copy each element at respective index. hence deep copy. Method 2: By assignment operator. Simply assigning the new vector to old one copies the vector. This way of assignment is not Assign vector content Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly.

In the range version (1), the new contents are elements constructed from each of the elements in the range between first and last, in the same order. It seems that a copy assignation of a vector doesn't mean a copy assignment of its elements. What if I have a class where copy construction and copy assignation must have different semantics?

EDIT. v2 is empty before the call to v2v1, but shouldn't the copy assignment be called after creating a vector with 2 elements?