C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm Functions is_permutation()C++ Algorithm is_permutation() function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching even if in different order. The first range is from [first1, last1) and the second starts from first2. Syntaxtemplate<class ForwardIterator1, class ForwardIterator2> bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator first2, BinaryPredicate pred); Parameterfirst1: It is an input iterator to the first element of the [first1, last1). last1: It is an input iterator to the last element of the [first1, last1). first2: It is an input iterator to the first element of the [first2, last2). pred: It is a binary function that accepts two elements as arguments and performs the task designed by the function. Return valueThe function returns the value true if all the elements in both the containers match even if in different order, otherwise it returns false. Example#include<iostream> #include<algorithm> #include<array> int main() { std::array<int, 5> a={6,7,8,9,10}; std::array<int, 5> b={10,8,7,6,9}; if(std::is_permutation(a.begin(),a.end(),b.begin())) std::cout<<"a and b have same elements.\n"; return 0; } Output: a and b have same elements. ComplexityThe function has linear complexity from the first1 element to the last1 element. Data racesObjects in both ranges are accessed. ExceptionsThe function throws an exception if any of the argument throws one.
Next TopicC++ Algorithm mismatch Function
|