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.
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.
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
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.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples