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

std::next_permutation

Defined in header <algorithm>

?

?

template< class BidirIt > bool next_permutation( BidirIt first, BidirIt last );

(1)

?

template< class BidirIt, class Compare > bool next_permutation( BidirIt first, BidirIt last, Compare comp );

(2)

?

变换范围[first, last)从所有按字典顺序排列的排列的集合中进入下一个排列。operator<comp.返回true如果存在这种置换,则将范围转换为第一个置换%28,如果std::sort(first, last)%29及回报false...

参数

first, last

-

the range of elements to permute

comp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ?true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type BidirIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

-Bidirit必须符合价值可互换和双向投资者的要求。

返回值

true如果新的排列比旧的要大的话。false如果到达最后一个置换,并且将范围重置为第一个置换。

例外

迭代器操作或元素交换引发的任何异常。

复杂性

最多是N/2交换,其中N =std::distance(first, last)...

可能的实施

模板<class BidirIt>Bool Next[医]置换%28 Bidiit First,Bidiit持续%29(if%28first==最后%29返回false;bidirit i=Lest;if%281=-i%29返回false;而%28 true%29{Bidiit 1,i2;i1=i;if%28)%2A---我<%2AI1%29{i2=最后;而%28%21%28%2A我<%2A-i2%29%29;性病::[医]交换%28i,i2%29;std::反向%28i1,最后%29;返回true;}如果%28i==First%29{std::反向%281,最后%29;返回false;}

*。

下面的代码打印字符串“ABA”的所有三个排列

二次

代码语言:javascript
复制
#include <algorithm>
#include <string>
#include <iostream>
 
int main()
{
    std::string s = "aba";
    std::sort(s.begin(), s.end());
    do {
        std::cout << s << '\n';
    } while(std::next_permutation(s.begin(), s.end()));
}

二次

产出:

二次

代码语言:javascript
复制
aab
aba
baa

二次

另见

is_permutation (C++11)

determines if a sequence is a permutation of another sequence (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