Open In App

std::is_permutation in C++ STL

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The C++ function std::algorithm::is_permutation() tests whether a sequence is permutation of other or not. It uses operator == for comparison. This function was defined in C++11.

Syntax:

template <class ForwardIterator1, class ForwardIterator2 >
bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);

first1, last1: Input iterators to the initial
and final positions of the first sequence.
first2 : Input iterator to the initial position of the second sequence.

Return value :
true : if all the elements in range [first1, last1]
compare equal to those of the range
starting at first2 in any order.
false : Any element missing or exceeding.

The function considers as many elements of this sequence as those in the range [first1, last1]. If this sequence is shorter, it causes undefined behaviour.

CPP
// CPP program to check if 
// two arrays are equal or not
// using std :: is_permutation
#include <iostream>
#include <algorithm>

//Driver Code
int main()
{
	int A[] = {1, 7, 0, 2};
	int B[] = {0, 7, 2, 1};
	
	// Check if array B includes all elements of 
	// array A
	if ( std :: is_permutation ( A, A+4, B ) )
	{
		std :: cout << "B is a permutation of A" ;
	}
	
	else
	{
		std :: cout << "B is not a permutation of A" ;
	}
	return 0;
}

Output:

B is a permutation of A

Other approach to find if arrays are equal or not is discussed here.

Another Example: Check whether two strings are anagram of each other

CPP
// CPP program to check whether two strings 
// are anagram of each other
// using std :: is_permutation
#include <iostream>
#include <algorithm>

/*Driver Code*/
int main()
{
    std :: string A = "SILENT";
    std :: string B = "LISTEN";
    
    /*Checking if B is a permutation of A*/
    if ( is_permutation ( A.begin(), A.end(), B.begin() ) )
    {
        std :: cout << "Anagrams" ;
    }
    
    else
    {
        std :: cout << "Not Anagrams" ;
    }
    return 0;
}

Output:

Anagrams

Other approach to Check whether two strings are anagram of each other is discussed here.

Versions of std::permutation

template< class ForwardIt1, class ForwardIt2 >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );
// (since C++11)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPredicate p );
// (since C++11)
template< class ForwardIt1, class ForwardIt2 >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
// (since C++14)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last)2,
BinaryPredicate p );
//(since C++14)

first1, last1 : the range of elements to compare
first2, last2 : the second range to compare
p : binary predicate which returns ?true if the elements should be treated as equal.

Examples:

CPP
// False
is_permutation ( c1.begin(),     c1.end (), c2.begin(), c2.end ()) 

// True
is_permutation ( c1.begin() + 1, c1.end (), c2.begin(), c2.end ())

// True, all empty ranges are permutations of each other
is_permutation ( c1.end (), c1.end (), c2.end(), c2.end ())

Reference: Official C++ Documentation for std :: is_permutation


is_permutation() in C++ STL
Visit Course explore course icon
Article Tags :
Practice Tags :

Similar Reads