std::is_permutation
STD::是[医]排列
Defined in header | | |
---|---|---|
template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 | (1) | (since C++11) |
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, BinaryPredicate p | (2) | (since C++11) |
template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 | (3) | (since C++14) |
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p | (4) | (since C++14) |
回报true
如果存在范围内的元素排列[first1, last1)
,这使得该范围等于范围。[first2,last2)
,在哪里last2
表示first2 + (last1 - first1)
如果没有人给的话。
1,3%29元素的比较operator==
如果行为不是等价关系...
使用给定的二进制谓词对2,4%29元素进行比较p
如果行为不是等价关系,则行为是未定义的。
参数
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. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a, const Type &b Type should be the value type of both ForwardIt1 and ForwardIt2. The signature does not need to have const &, but the function must not modify the objects passed to it. |
类型要求
-前进1,前进2必须符合先行者的要求。
-ForwardIt 1,ForwardIt 2必须具有相同的值类型。
返回值
true
如果范围[first1, last1)
是范围的排列[first2, last2)
...
复杂性
最多为O%28N2%29的谓词应用程序,或者如果序列已经相等,则精确N,其中N=
std::distance
(first1, last1)
...
但是如果ForwardIt1
和ForwardIt2
满足…的要求RandomAccessIterator
和std::distance
(first1, last1)
!=
std::distance
(first2, last2)
不进行谓词的应用。
可能的实施
模板<类ForwardIt 1,类ForwardIt 2>bool是[医]排列%28ForwardIt1优先,ForwardIt 1最后,ForwardIt2d[医]第一%29{//跳过通用前缀STD::Tie%281,d[医]第一%29=STD::不匹配%281,最后,d[医]第一个%29;//迭代其余的元素,计算每个元素//从元素的次数。[首先,最后%29出现在。[丁[医]第一,d[医]最后%29如果%28First%21=最后%29(ForwardIt2d)[医]最后=d[医]第一;性病::预付%28d[医]最后,std::距离%281,最后%29%29;对于%28 ForwardIt 1 i=先;i%21=Lest;++i%29{if%28i%21=std::查找%28First,i,%2AI%29%29继续;//已计算此%2Ai AUTO m=STD::Count%28d[医]第一,d[医]最后,%2Ai%29;if%28m=0最后,数%28 i,%2AI%29%21=m%29{返回false;}}返回true;}
*。
例
二次
#include <algorithm>
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v1{1,2,3,4,5};
std::vector<int> v2{3,5,4,1,2};
std::cout << "3,5,4,1,2 is a permutation of 1,2,3,4,5? "
<< std::boolalpha
<< std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n';
std::vector<int> v3{3,5,4,1,1};
std::cout << "3,5,4,1,1 is a permutation of 1,2,3,4,5? "
<< std::boolalpha
<< std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';
}
二次
产出:
二次
3,5,4,1,2 is a permutation of 1,2,3,4,5? true
3,5,4,1,1 is a permutation of 1,2,3,4,5? false
二次
另见
next_permutation | generates the next greater lexicographic permutation of a range of elements (function template) |
---|---|
prev_permutation | generates the next smaller lexicographic permutation of a range of elements (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。