当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::is_permutation用法及代码示例


C++函数std::algorithm::is_permutation()测试一个序列是否是其他序列的置换。它使用运算符==进行比较。此函数在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. 

返回值:
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 <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; 
}

输出:


B is a permutation of A

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

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

// 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; 
}

输出:

Anagrams

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

std::permutation的版本

template
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2 );
// (since C++11)
template
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, BinaryPredicate p );
// (since C++11)
template
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, ForwardIt2 last2 );
// (since C++14)
template
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 ())

参考: Official C++ Documentation for std::is_permutation



相关用法


注:本文由纯净天空筛选整理自 std::is_permutation in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。