// prev_permutation.cpp -- 2011-10-05-06.23
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using std ::vector ;
using std ::less ;
template<class T>
class Print
{
public:
void operator () (const T & t) const
{
std ::cout << t << " " ;
}
} ;
int _tmain(int argc, _TCHAR* argv[])
{
vector<char> vec1(3) ;
vec1[0] = 'c' ;
vec1[1] = 'b' ;
vec1[2] = 'a' ;
// prev_permutation (beg,end) ;
// 操作前:[beg,end)标示输入序列.
// 操作后:将输入序列替换成按字典序排序的上一个序列.
// 返回值:如果输入序列已为最前一个排列,返回false.
// 否则返回true.
// 备注: 使用<操作符比较元素.
while (prev_permutation(vec1.begin(), vec1.end()))
{
for_each(vec1.begin(), vec1.end(), Print<char> ()) ;
std ::cout << std ::endl ;
}
vector<int> vec2(3) ;
vec2[0] = 3 ;
vec2[1] = 2 ;
vec2[2] = 1 ;
// prev_permutation (beg,end, comp) ;
// 操作前:[beg,end)标示输入序列.comp是二元函数对象.
// 操作后:将输入序列替换成按字典序排序的上一个序列.
// 返回值:如果输入序列已为最前一个排列,返回false.
// 否则返回true.
// 备注: 使用comp操作符比较元素.
while (prev_permutation(vec2.begin(), vec2.end(), less<int> ()))
{
for_each(vec2.begin(), vec2.end(), Print<int> ()) ;
std ::cout << std ::endl ;
}
std ::cin.get() ;
return 0 ;
}
prev_permutation
最新推荐文章于 2023-10-22 19:02:22 发布