C++ std::iter_swap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::iter_swap(v.begin(), v.end() - 1);
std::iter_swap(it1, it2);
CallWhat 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
C++
template<class ForwardIt1, class ForwardIt2>
void iter_swap(ForwardIt1 a, ForwardIt2 b);

Parameters

ParameterDescription
a, bRequired. Two iterators that point to valid elements. The element types must be swappable.

Return Value

Return ValueDescription
voidNothing is returned. The two pointed-to elements exchange values.

C++ Examples

Example 1 — Swap first and last

Exchange the two ends of a vector.

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

  1. v.begin() points to 1 and v.end() - 1 points to 4.
  2. iter_swap() exchanges those two values.
  3. The middle elements are not touched.

Example 2 — Swap across containers

Swap elements that live in different containers.

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

  1. One iterator comes from a vector and the other from a list.
  2. iter_swap() only needs two iterators whose elements can be swapped.
  3. 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.

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

  1. max_element() returns an iterator to 9.
  2. iter_swap() swaps it with the element at the front, which is 3.
  3. This is one step of a selection sort.

Frequently Asked Questions

In C++, std::iter_swap() swaps the values that two iterators point to. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The elements the two iterators point at are exchanged.
swap() takes two objects. iter_swap() takes two iterators and swaps the elements they point to.
Yes, as long as the element types can be swapped with each other.

Next: lexicographical_compare()

Checks whether one range comes before another in dictionary order.

lexicographical_compare() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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