Chapter 20. Mutating

Table of Contents

swap
Specializations

swap

Specializations

If you call std::swap(x,y); where x and y are standard containers, then the call will automatically be replaced by a call to x.swap(y); instead.

This allows member functions of each container class to take over, and containers' swap functions should have O(1) complexity according to the standard. (And while "should" allows implementations to behave otherwise and remain compliant, this implementation does in fact use constant-time swaps.) This should not be surprising, since for two containers of the same type to swap contents, only some internal pointers to storage need to be exchanged.