Home C++ Algorithms Functions iter_swap() C++ std::iter_swap() Function Definition In C++, std::iter_swap() takes two iterators and swaps the elements they point at. It is the iterator version of std::swap().
The iterators can point into the same container or two different ones. It is often used inside other algorithms, such as when moving the largest element to the front.
Syntax Add #include <algorithm>, then call std::iter_swap(). Start with these two lines.
std::iter_swap(v.begin(), v.end() - 1);
std::iter_swap(it1, it2); Call What it does std::iter_swap(v.begin(), v.end() - 1);Swap the first and the last element. std::iter_swap(it1, it2);Swap whatever two iterators point to.
Library declarations template<class ForwardIt1, class ForwardIt2>
void iter_swap(ForwardIt1 a, ForwardIt2 b); Parameters Parameter Description a, b Required. Two iterators that point to valid elements. The element types must be swappable.
Return Value Return Value Description voidNothing is returned. The two pointed-to elements exchange values.
C++ Examples 1. Swap first and last 2. Swap across containers 3. Move the largest to the front Example 1 — Swap first and last Exchange the two ends of a vector.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::iter_swap(v.begin(), v.end() - 1);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works v.begin() points to 1 and v.end() - 1 points to 4.iter_swap() exchanges those two values.The middle elements are not touched. Example 2 — Swap across containers Swap elements that live in different containers.
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
int main() {
std::vector<int> v = {10, 20};
std::list<int> l = {5, 6};
std::iter_swap(v.begin(), l.begin());
std::cout << "v: ";
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
std::cout << "l: ";
for (int x : l) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works One iterator comes from a vector and the other from a list. iter_swap() only needs two iterators whose elements can be swapped.The first element of each container changed places. Example 3 — Move the largest to the front Find the biggest value and swap it to index 0.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 9, 4, 1};
auto it = std::max_element(v.begin(), v.end());
std::iter_swap(v.begin(), it);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works max_element() returns an iterator to 9.iter_swap() swaps it with the element at the front, which is 3.This is one step of a selection sort. Frequently Asked Questions What does std::iter_swap() do? In C++, std::iter_swap() swaps the values that two iterators point to. Include <algorithm> to use it.
Which header and C++ version do I need for std::iter_swap()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::iter_swap() return? It returns nothing (void). The elements the two iterators point at are exchanged.
What is the difference between swap() and iter_swap()? swap() takes two objects. iter_swap() takes two iterators and swaps the elements they point to.
Can the two iterators be in different containers? Yes, as long as the element types can be swapped with each other.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion