std::swap is used for swapping of elements between two containers. One of the other way of doing this same thing is facilitated by
std::iter_swap, which as the name suggests is used for swapping the elements with the help of an iterator.
It simply exchanges the values of the elements pointed to by the iterators. If we look at its internal working, we will find that this function itself uses
std::swap().
Syntax:
void iter_swap (ForwardIterator1 a, ForwardIterator2 b);
Here, a and b are forward iterators.
Returns: It has a void return type, so it does not
return any value.
CPP
// C++ program to demonstrate the use of std::iter_swap
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
// Declaring first vector
vector<int> v1;
int i;
for (i = 0; i < 10; ++i) {
v1.push_back(i);
}
// v1 contains 0 1 2 3 4 5 6 7 8 9
vector<int>::iterator i1, i2;
i1 = v1.begin();
i2 = v1.end() - 1;
// Performing swap between first and last element
// of vector
std::iter_swap(i1, i2);
// Displaying v1 after swapping
for (i = 0; i < 10; ++i) {
cout << v1[i] << " ";
}
return 0;
}
Output:
9 1 2 3 4 5 6 7 8 0
Here, in this program we have swapped elements from v1 with the help of two iterators, one of them pointing at the beginning of v1 and the other one pointing at the end of v1.
std::iter_swap vs std::swap
After coming to know that iter_swap is used to swap the values, just like std::swap(), the question now arises is why should we learn iter_swap, if we have already something called swap(). Some of the reasons in support of iter_swap are:
- Optimization for node-based sequences: Most STL algorithms operate on iterator ranges. It therefore makes sense to use iter_swap when swapping elements within those ranges, swapping the elements pointed to by two iterators. This allows optimization for node-based sequences such as std::list, whereby the nodes are just relinked, rather than the data actually being swapped.
- Use in STL definition: Some STL algorithm like std::reverse involve the use of std::iter_swap in its definition. Therefore, one should have knowledge about this in order to understand these definition.
CPP
// Definition of std::reverse()
template void reverse(BidirectionalIterator first,
BidirectionalIterator last)
{
while ((first != last) && (first != --last))
{
std::iter_swap(first, last);
++first;
}
}
- Providing abstraction: iter_swap usefully encapsulates the part of the swappable interface which you would otherwise implement every time.
Similar Reads
list::swap() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::swap()This function is used to swap the con
2 min read
set::swap() in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::swap() This func
2 min read
std::swap_ranges in C++ std::swap is used for swapping of elements between two containers. One of its variation is std::swap_ranges, which as the name suggests is used for swapping the elements within a range. It simply exchanges the values of each of the elements in the range [first1, last1) with those of their respective
4 min read
stack swap() in C++ STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end and (top) an element is removed from that end only.stack::swap()This function is used to swap the contents of one stack with another stack of same type but the size may vary. Sy
2 min read
queue::swap() in C++ STL Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() fun
2 min read