ruk·si

C++
Swap

Updated at 2013-07-07 20:39

Swap function.

// bad
void swap(int* v1, int* v2)
{
    int temp = *v1;
    *v1 = *v2;
    *v2 = temp;
}
// Called with awkward swap(&i, &j) and swappables can be NULLs.

// good
void swap(int& v1, int& v2)
{
    int temp = v1;
    v1 = v2;
    v2 = temp;
}
// Called with swap(i, j) and swappables cannot be NULLs.