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

std::prev_permutation

Defined in header <algorithm>

?

?

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

(1)

?

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

(2)

?

变换范围[first, last)从所有按字典顺序排列的排列集合中转换到前面的置换中。operator<comp.返回true如果存在这种置换,则将范围转换为最后一次置换%28,如果std::sort(first, last); std::reverse(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如果达到第一个置换,并且范围被重置为最后一个置换。

例外

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

复杂性

顶多(last-first)/2交换。

可能的实施

模板<class BidirIt>布尔普雷夫[医]置换%28 Bidiit First,Bidiit持续%29(if%281==最后%29返回false;bidiit i=Lest;if%281=-i%29返回false;而%281%29{Bidiit 1,i2;i1=i;if%28%2Ai1<%2A-i%29{i2=最后;而%28%21%28%2A--i2<%2AI%29%29;性病::[医]交换%28i,i2%29;std::反向%28i1,最后%29;返回true;}如果%28i==First%29{std::反向%281,最后%29;返回false;}

*。

下面的代码以相反的顺序打印字符串“abc”的所有六个排列。

二次

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

二次

产出:

二次

代码语言:javascript
复制
cba cab bca bac acb abc

二次

另见

is_permutation (C++11)

determines if a sequence is a permutation of another sequence (function template)

next_permutation

generates the next greater lexicographic permutation of a range of elements (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com