📜  C++ STL中的std :: is_permutation

📅  最后修改于: 2021-05-30 17:42:23             🧑  作者: Mango

C++函数std :: algorithm :: is_permutation()测试一个序列是否是其他序列的置换。它使用运算符==进行比较。此函数在C++ 11中定义。
句法:

template 
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.

该函数考虑此序列中与[first1,last1]范围内的元素一样多的元素。如果此序列较短,则会导致不确定的行为。

// CPP program to check if 
// two arrays are equal or not
// using std :: is_permutation
#include 
#include 
  
//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;
}

输出:

B is a permutation of A

此处讨论了其他用于确定数组是否相等的方法。

另一个示例:检查两个字符串是否彼此变位

// CPP program to check whether two strings 
// are anagram of each other
// using std :: is_permutation
#include 
#include 
  
/*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;
}

输出:

Anagrams

本文讨论了检查两个字符串是否彼此相似的另一个方法。

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.

例子:

// 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 ())

参考: std :: is_permutation的官方C++文档

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”