首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::is_permutation

Defined in header <algorithm>

?

?

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

但是如果ForwardIt1ForwardIt2满足…的要求RandomAccessIteratorstd::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;}

*。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
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)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com