This sample is licensed under the CC0 Public Domain Dedication.
Swap the contents of two containers.
On line 10, we swap the contents of the two
std::vectors using the
std::swap_ranges algorithm, which
is given the beginning and end iterators of one range and the
beginning iterator of a second range. This performs an
element-by-element swap of their values (in this case, four swaps).
The second range must have at least the number of elements in the
first range. After this operation, iterators remain valid and point
at the same container as they did previously.
On line 12, we use
swap member function to swap the
contents all in one shot. Alternatively, lines 14–15 perform the same
operation with the generic swap pattern.
This approach is much faster than
std::swap_ranges, as it simply
swaps over the internal storage in constant time. It also does not
require the containers to have the same size. However, after the
operation, iterators will now point at the other container and
past-the-end iterators are invalidated.