Example of swap() in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 5;
int y = 10;
cout << "Value of x before swapping: " << x << endl;
cout << "Value of y before swapping: " << y << endl;
swap(x, y);
cout << "Value of x: " << x << endl;
cout << "Value of y: " << y << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run CodeOutput:
Value of x before swapping: 5
Value of y before swapping: 10
Value of x: 10
Value of y: 5

You can also try this code with Online C++ Compiler
Run Code
You can try and compile with the help of online c++ compiler.
How to Swap Two Numbers Using Function in C++?
To swap two numbers using a function in C++, you can either define your own swap function or utilize the standard swap() function from the <algorithm> header. Here's how you can implement both methods:
Using a User-defined Swap Function
C++
#include <iostream>
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int num1 = 5, num2 = 10;
std::cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
swap(num1, num2);
std::cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
Before swapping: num1 = 5, num2 = 10
After swapping: num1 = 10, num2 = 5
Using the Standard swap() Function
C++
#include <iostream>
#include <algorithm>
int main() {
int num1 = 5, num2 = 10;
std::cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
std::swap(num1, num2);
std::cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
Before swapping: num1 = 5, num2 = 10
After swapping: num1 = 10, num2 = 5
Both methods achieve the same result of swapping the values of num1 and num2, demonstrating the simplicity and effectiveness of using functions to perform such operations in C++.
Frequently Asked Questions
What library is swap in C++?
The swap() function in C++ is defined in the <algorithm> header of the C++ Standard Library. It provides a convenient and efficient way to swap the values of two variables or elements.
How to use swap string function in C++?
To swap the contents of two strings in C++, you can simply use the swap() function from the <algorithm> header. For example, std::string str1 = "Hello"; std::string str2 = "World"; std::swap(str1, str2); swaps the contents of str1 and str2.
How to swap words in C++?
To swap words in C++, you can use string manipulation techniques. For example, you can tokenize the input string into words, swap the desired words, and then concatenate them back together. Alternatively, you can use string functions like substr() and find() to extract and rearrange substrings.
Conclusion
In this article, we have extensively discussed the Swap() Function in C++. The swap() function in C++ stands as a powerful tool for efficiently exchanging the values of variables, providing a simple and optimized solution for tasks requiring value interchange. Whether it's swapping numeric values, strings, or even user-defined objects, understanding and leveraging the swap() function empowers C++ programmers to write cleaner, more concise, and more efficient code.