Last active
July 7, 2016 09:44
-
-
Save congdanhqx-zz/ce8f29f0708ca4b84bd6b5d3c43370fb to your computer and use it in GitHub Desktop.
Example for WordPress Post https://congdanhqx.wordpress.com/2016/07/07/hey-guys-dont-try-to-be-smart-with-swap/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <memory> | |
int a; | |
int b; | |
void swap1(void) | |
{ | |
int t = a; | |
a = b; | |
b = t; | |
} | |
// Don't try to be smart guy | |
void swap2(void) | |
{ | |
a ^= b; | |
b ^= a; | |
a ^= b; | |
} | |
// Don't try to be smart guy | |
void swap3(void) | |
{ | |
b += a; | |
a = b - a; | |
b -= a; | |
} | |
// Obviously, this can be done only in C++ | |
void swap4(void) | |
{ | |
std::swap(a, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment