C++ std::is_permutation() Function

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

Definition

In C++, std::is_permutation() returns true when the second range holds the same elements as the first, in any order. It is a good test for anagrams.

Duplicates count: both ranges must have the same number of each value.

Syntax

Add #include <algorithm>, then call std::is_permutation(). Start with these two lines.

C++
std::is_permutation(a.begin(), a.end(), b.begin());
std::is_permutation(a.begin(), a.end(), b.begin(), b.end());
CallWhat it does
std::is_permutation(a.begin(), a.end(), b.begin());Compare a with the same number of elements from b.
std::is_permutation(a.begin(), a.end(), b.begin(), b.end());Also checks that the lengths match (C++14).
Library declarations
C++
template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2);

// C++14
template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first1, ForwardIt1 last1,
                    ForwardIt2 first2, ForwardIt2 last2);

Parameters

ParameterDescription
first1, last1Required. The first range.
first2, last2The second range. With only first2 it must be at least as long as the first.
predOptional. A function that says if two elements are equal.

Return Value

Return ValueDescription
trueThe second range is a rearrangement of the first.
falseThe two ranges do not hold the same elements.

C++ Examples

Example 1 — Same numbers

Check that two vectors hold the same numbers.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 3, 4};
    std::vector<int> b = {4, 2, 1, 3};

    bool ok = std::is_permutation(a.begin(), a.end(), b.begin(), b.end());

    std::cout << std::boolalpha << "Permutation: " << ok << '\n';
    return 0;
}

How It Works

  1. Both vectors hold 1, 2, 3 and 4.
  2. The order does not matter for this check.
  3. Each number of a has a partner in b, so the answer is true.

Example 2 — Different counts

The counts of repeated values must match.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 2};
    std::vector<int> b = {1, 1, 2};

    bool ok = std::is_permutation(a.begin(), a.end(), b.begin(), b.end());

    std::cout << std::boolalpha << "Permutation: " << ok << '\n';
    return 0;
}

How It Works

  1. Both vectors have three numbers made of 1 and 2.
  2. a has two 2s and one 1, but b has two 1s and one 2.
  3. The counts differ, so the answer is false.

Example 3 — Anagram check

Two words are anagrams if their letters can be rearranged.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string a = "listen";
    std::string b = "silent";

    bool anagram = std::is_permutation(a.begin(), a.end(), b.begin(), b.end());

    std::cout << a << " and " << b << (anagram ? " are anagrams" : " are not anagrams") << '\n';
    return 0;
}

How It Works

  1. Strings are ranges of characters.
  2. Both words use the letters e, i, l, n, s and t once.
  3. So one is a rearrangement of the other.

Frequently Asked Questions

In C++, std::is_permutation() checks whether one range is a rearrangement of another. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns true if the second range is a permutation (a rearrangement) of the first range.
Call std::is_permutation() on the two strings. It returns true when they use the same letters the same number of times.
No. It works on any order.

Next: is_sorted()

Checks whether a range is sorted.

is_sorted() 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.

17 people found this page helpful