How to Provide a Swap Function for My Class in C++?
Last Updated :
07 Aug, 2024
In C++, providing a swap function for the class allows efficient swapping of the objects of that class type which can be important for many algorithms like sorting algorithms or container manipulations. In this article, we will learn how to implement a swap function for our own user defined class.
swap Function for User Defined Class in C++
There are two ways in which we can provide swap function for our class.
- By defining the whole swap logic by ourselves.
- By using the inbuild swap function where we can.
The second method is better as it provides a concise and efficient method of swapping in most of the cases.
Implementing the swap Function using std::swap
To provide the swap function for your class using std::swap, follow the below steps:
- Define the swap function inside your class.
- Provide a non-member swap function that calls the member swap function.
- Optionally, provide a specialized std::swap function template for your class.
Need of Custom Swap
Function?
By default, the STL uses std::swap to exchange the values of two objects. While std::swap works well for many cases, it may not be the most efficient for your custom class. Implementing a custom swap function allows you to control how member variables are exchanged, potentially improving performance by avoiding unnecessary copying or allocation operations.
C++ Program to Implement Swap Function
Here’s how you can implement a swap function for the custom class:
C++
// C++ program to implement swap function
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(int a, double b) : data1(a), data2(b)
{
}
// Member swap function
void swap(MyClass &other) noexcept
{
// Swap data1 members
std::swap(data1, other.data1);
// Swap data2 members
std::swap(data2, other.data2);
}
// Display function for demonstration purposes
void display() const
{
cout << "data1: " << data1 << ", data2: " << data2 << endl;
}
private:
int data1;
double data2;
};
// Non-member swap function for better accessibility
void swap(MyClass &first, MyClass &second) noexcept
{
first.swap(second);
}
int main()
{
MyClass obj1(1, 1.1), obj2(2, 2.2);
cout << "Before swap:" << endl;
// Display obj1 before swap
obj1.display();
// Display obj2 before swap
obj2.display();
// Swap the objects
swap(obj1, obj2);
cout << "After swap:" << endl;
// Display obj1 after swap
obj1.display();
// Display obj2 after swap
obj2.display();
return 0;
}
OutputBefore swap:
data1: 1, data2: 1.1
data1: 2, data2: 2.2
After swap:
data1: 2, data2: 2.2
data1: 1, data2: 1.1
Benefits of Providing a swap Function
- Performance: The Swapping objects using swap is often more efficient than copying objects.
- Standard Library Compatibility: Many standard library algorithms rely on the efficient swapping such as the sorting and containers like std::vector and std::map.
- Exception Safety: The swap can facilitate exception-safe code by the enabling the copy-and-swap idiom.
Similar Reads
Passing Pointers to Functions In C++ Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any varia
4 min read
unordered_multiset swap() function in C++ STL The unordered_multiset::swap() is a built-in function in C++ STL which swaps the contents of two unordered_multiset containers. Note: Both of the containers should have the same type of elements. Sizes of the containers may differ. Syntax: unordered_multiset1.swap(unordered_multiset2); Parameters: T
3 min read
multimap swap() function in C++ STL The multimap::swap() is a built-in function in C++ STL which swaps two multimap container. The contents of multimap1 are in multimap2 and contents of multimap2 are in multimap1 after swap() function is called. Syntax: multimap1_name.swap(multimap2_name) Parameters: This function accepts one paramete
2 min read
How to quickly swap two arrays of same size in C++? Given two arrays a[] and b[] of same size, we need to swap their contents. Example : Input: a[] = {1, 2, 3, 4} b[] = {5, 6, 7, 8}Output: a[] = {5, 6, 7, 8}b[] = {1, 2, 3, 4} A simple solution is to iterate over elements of both arrays and swap them one by one. A quick solution is to use std::swap().
2 min read
unordered_set swap() function in C++ STL The unordered_set::swap() method is a builtin function in C++ STL which is used to exchange values of two unordered_set containers. It swaps the element of two unordered_set containers. The sizes may differ but it swaps elements and changes the order of elements also.Syntax: unordered_set_firstname.
2 min read
unordered_multimap swap() function in C++ STL The unordered_multimap::swap() is a built-in function in C++ STL which swaps the contents of two unordered_multimap containers. The sizes can differ of both the containers. Syntax: unordered_multimap_name1.swap(unordered_multimap_name2) Parameters: The function accepts a single mandatory parameter u
3 min read