[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppSwap
Example:
Below is the code I retrieved from the header file algorithm.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppSwap
(C++) std::swap
Used for swapping two variables. Can be found in the STL header file algorithm.h.Example:
int main() { int a = 10; int b = 20; swap(a,b); assert(a==20 && b==10); }
- include <cassert>
Below is the code I retrieved from the header file algorithm.
template <class T>
inline void swap (T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
XOR swap
A less common way of swapping two integer values. Note that it does not require a temporary variable; however, in practice it can be slower than simply assigning, as the compiler can use a CPU register for the temporary rather than actual memory.
void xorSwap (int &x, int &y)
{
x ^= y;
y ^= x;
x ^= y;
}
'swap' links
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
