C++ std::is_permutation() Function

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.
std::is_permutation(a.begin(), a.end(), b.begin());
std::is_permutation(a.begin(), a.end(), b.begin(), b.end());
| Call | What 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
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
| Parameter | Description |
|---|
| first1, last1 | Required. The first range. |
| first2, last2 | The second range. With only first2 it must be at least as long as the first. |
| pred | Optional. A function that says if two elements are equal. |
Return Value
| Return Value | Description |
|---|
true | The second range is a rearrangement of the first. |
false | The two ranges do not hold the same elements. |
C++ Examples
Example 1 — Same numbers
Check that two vectors hold the same numbers.
#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
- Both vectors hold 1, 2, 3 and 4.
- The order does not matter for this check.
- 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.
#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
- Both vectors have three numbers made of 1 and 2.
a has two 2s and one 1, but b has two 1s and one 2.- The counts differ, so the answer is
false.
Example 3 — Anagram check
Two words are anagrams if their letters can be rearranged.
#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;
}
listen and silent are anagrams
How It Works
- Strings are ranges of characters.
- Both words use the letters e, i, l, n, s and t once.
- 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.
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.
17 people found this page helpful