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().
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.
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.
// Definition of std::reverse()
((first != last) && (first != --last))
- Providing abstraction: iter_swap usefully encapsulates the part of the swappable interface which you would otherwise implement every time.
This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- C++ Program for the Fractional Knapsack Problem
- Lexicographical smallest number after at most K consecutive swaps
- Important functions of STL Components in C++
- Check if Array elements can be maximized upto M by adding all elements from another array
- How to find index of a given element in a Vector in C++
- Print all lexicographical greater permutations of a given string
- std::remove_const in C++ with Examples
- All possible values of floor(N/K) for all values of K
- Finding Median of unsorted Array in linear time using C++ STL
- std::string::crbegin() and std::string::crend() in C++ with Examples
- std::is_trivially_copy_assignable class in C++ with Examples
- Find the K closest points to origin using Priority Queue
- How to iterate through a Vector without using Iterators in C++
Improved By : nidhi_biet