當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。