How to Swap Two Numbers in C++
Swapping two values — making a hold what b had and vice versa — is one of the first building blocks you’ll use in sorting, shuffling, and countless algorithms. C++ gives you several ways to do it, from a simple temporary variable to the built-in std::swap.
Method 1: Using a Temporary Variable
This is the classic, foolproof approach. You can’t just write a = b; b = a; because the first line destroys the original value of a before you can save it. A temporary variable holds it safely:
#include <iostream>
int main() {
int a = 5, b = 10;
std::cout << "Before: a = " << a << ", b = " << b << "\n";
int temp = a; // save a
a = b; // a now holds b's value
b = temp; // b now holds a's old value
std::cout << "After: a = " << a << ", b = " << b << "\n";
return 0;
}
Output shows a = 10, b = 5. This works for any type and is impossible to get wrong once you understand why the temp is needed.
Method 2: Using std::swap
Modern C++ ships with a ready-made function that does exactly this. It lives in <utility> (and is included by many other headers automatically):
#include <iostream>
#include <utility>
int main() {
int a = 5, b = 10;
std::swap(a, b);
std::cout << "a = " << a << ", b = " << b << "\n"; // a = 10, b = 5
return 0;
}
This is the recommended way in real code. It’s clear, works on strings, vectors, and custom types, and the compiler optimizes it well. Why reinvent something the standard library already does safely?
Method 3: Writing Your Own Swap Function
To swap values inside a function so the change is visible to the caller, you must use references. Passing by value would only swap local copies:
#include <iostream>
void swapValues(int& x, int& y) { // references, not copies
int temp = x;
x = y;
y = temp;
}
int main() {
int a = 1, b = 2;
swapValues(a, b);
std::cout << "a = " << a << ", b = " << b << "\n"; // a = 2, b = 1
return 0;
}
The & makes x and y aliases for the caller’s actual variables. Drop the & and the swap would have no effect outside the function — a very common beginner bug.
Method 4: Without a Temporary Variable
You can swap using arithmetic, which looks clever but has real downsides:
int a = 5, b = 10;
a = a + b; // a = 15
b = a - b; // b = 5
a = a - b; // a = 10
This avoids a temp variable, but for very large values a + b can overflow and produce wrong results. It’s also harder to read. Interview questions sometimes ask for it, but in actual programs you should prefer std::swap or a temporary variable — clarity beats cleverness.
Which Method Should You Use
For everyday code, reach for std::swap — it’s clear, safe, and works on every type. Use a temporary variable when you want to show the mechanics explicitly or in a learning context. Use references when swapping inside your own functions. Save the no-temp arithmetic trick for puzzles, not production.
Related Articles
- C++ Pass by Value vs Reference — why references matter for swapping
- C++ Reference vs Pointer — two ways to refer to a variable
- C++ Variables and Data Types — the basics of storing values
- C++ Functions Tutorial — parameters and arguments
- C++ Sort Algorithm — where swapping powers sorting
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.