To swap the values of two variables, x and y, using a temporaryvariable t:t = x;x = y;y = t;To implement this algorithm as a function, the variables must bepassed by reference. For efficiency, particularly with large orcomplex objects that are expensive to copy, use move semantics. TheC++ standard library provides the following implementation (sinceC++11):namespace std {template<typename T>void swap (T&& x, T&& y) {T t = std::move (x);x = std::move (y);y = std::move (t);}};The C++ standard library swap function (std::swap) uses theabove implementation (since C++11). If a type does not support themove semantic, copy semantics will be used instead.