Assign and Swap

This lesson deals with ways to update and swap values in containers.

We can assign new elements to an existing container and, if required, swap two containers as well. If we want to assign a container cont2 to another container cont1, we can do so either through copy assignment cont1= cont2 or move assignment cont1= std::move(cont2). In a move assignment, the value of cont2 is copied to cont1 and cont2 becomes empty. A special form of assignment is the one with an initializer list: cont= {1, 2, 3, 4, 5}. In the case of std:array, an initializer list is not possible, hence we use aggregate initialization. The function swap exists in two forms. It is a method cont1(swap(cont2)) and also a function template std::swap(cont1, cont2).

Get hands-on with 1200+ tech skills courses.