C++ STL中的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: 第一个序列的初始和最终位置的输入迭代器。
first2 : 第二个序列的初始位置的输入迭代器。
返回值 :
true : 如果[first1,last1]范围内的所有元素与以任意顺序从first2开始的范围的元素相等。
false : 存在任何缺失或超出的元素。
该函数将考虑该序列的元素数量与[first1,last1]范围内的数量相同。如果序列较短,则会导致未定义行为。
输出:
还有另一种方法来检查数组是否相等,讨论在这里。
另一个例子: 检查两个字符串是否互为字谜
输出:
还有另一种方法可以检查两个字符串是否互为字谜,讨论在这里。
std :: permutation的版本
template< class ForwardIt1, class ForwardIt2 >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );
// (自C++11以来)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPredicate p );
// (自C++11以来)
template< class ForwardIt2 >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
// (自C++14以来)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last,
BinaryPredicate p );
//(自C++14以来)
first1, last1 : 要比较的元素范围
first2, last2 : 第二个要比较的范围
p : 二元谓词,如果将元素视为相等,则返回true。
例子: