Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jul 9, 2024
Difficulty: Easy

Swap() Function in C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Welcome to our blog on the swap() function in C++. In programming, efficiently swapping the values of two variables is a common task with various implementations. However, the standard swap() function in C++ provides a simple and optimized solution for this purpose. Understanding its usage, behavior, and underlying mechanisms is essential for every C++ programmer seeking to write efficient and maintainable code.

Swap() Function in C++

Let’s discuss this function of C++ programming in detail. Also see, Literals in C

What is the Swap() Function?

The swap() function in C++ is a standard library function used to exchange the values of two variables of the same type. It provides a convenient and efficient way to swap the contents of variables without requiring manual temporary variables or complex logic. The swap() function is commonly utilized in various scenarios to streamline code and improve readability, particularly in algorithms involving sorting, permutations, and resource management.

For example, if you are provided with two numbers and asked to arrange them in ascending order, this function will help you do this work much easier.

Syntax of swap() in C++

Std:: swap(x, y);

Parameters of swap() in C++

Two parameters are mandatory in the swap() function in C++; they can be of any data type.

Return Value of swap() in C++

The function swaps the two values and does not return anything.

Swap() function can include any parameters like string, int, float, and even data structures like arrays, stacks, etc.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

Output:

Value of x before swapping: 5
Value of y before swapping: 10
Value of x: 10
Value of y: 5

 

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++

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;
}

Output:

Before swapping: num1 = 5, num2 = 10
After swapping: num1 = 10, num2 = 5

Using the Standard swap() Function

  • C++

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;
}

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.

To learn, see Operating SystemUnix File SystemFile System Routingand File Input/Output.

Also check out this article - Pair in C++ and, Dynamic Binding in C++

Happy Learning!

Topics covered
1.
Introduction
2.
What is the Swap() Function?
2.1.
Syntax of swap() in C++
2.2.
Parameters of swap() in C++
2.3.
Return Value of swap() in C++
3.
Example of swap() in C++
4.
How to Swap Two Numbers Using Function in C++?
4.1.
Using a User-defined Swap Function
4.2.
C++
4.3.
Using the Standard swap() Function
4.4.
C++
5.
Frequently Asked Questions
5.1.
What library is swap in C++?
5.2.
How to use swap string function in C++?
5.3.
How to swap words in C++?
6.
Conclusion