This is a Code Nummy about swapping variables. Please check out the other Code Nummies.
Swapping variables is a common task in many programs. There are multiple ways to perform variable swapping and it is good to know about them.
- Note: Some of the solutions might take some creativity.
- Note: While it is good to know about the different possible solutions, it is important when not to use them.
This is the standard swap algorithm taught in every textbook.
Benefits:
- Works for every type
- Works in most programming languages
- Easy to understand
Drawbacks
- Temporary variable needed
Use the functionality provided by the standard library. See this stack overflow answer.
- std::swap cppreference.com
- std::exchange n3668
x, y = y, x
Benefits:
- Works for every type
- Very easy to understand
- great semantics
Drawbacks:
- Language specific
What if I tell you that you can swap two numbers without the use of a temporary, but just by using addition/subtraction? If you do not know the solution, this might be quite some fun to find out.
Benefits:
- No temporary needed
- Works in most programming languages
Drawbacks:
- Not intuitive to understand
- Bad semantics
- Only works for number types
If you dislike addition/subtraction for some reason, the same approach is possible with multiplication, division.
Benefits:
- No temporary needed
- Works in most programming languages
Drawbacks:
- Not intuitive to understand
- Bad semantics
- Only works for number types
- multiplication/division are slow
- Won't work if one of the values is zero
- Out of the four ways to swap variables, which one would you prefer when? And Why?
- Can you come up with another (fith) solution, which uses the xor operation?