Save 40% on books and eBooks + 70% on videos now through May 31*—use code PROGRAM. Shop now.
Suppose you want to swap vi and vj. The generic approach is to call the swap() algorithm:
vector <int> vj;
However, most container classes (as well as other types of swappable Standard Library entities such as shared_ptr) have a swap() member function:
vi.swap(vj); //preferable to std::swap(vi,vj);
Generally speaking, you should use the swap() member function instead of the generic std::swap() algorithm because the member function is optimized for its class type. In some cases, it uses move semantics instead of copying whole objects.
Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.