码迷,mamicode.com
首页 > 编程语言 > 详细

STL_算法(17)_排列组合 next_permutation() perv_permutation()

时间:2016-08-17 09:04:05      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

next_permutation()

prev_permutation()

#include<iostream>
#include<algorithm>
#include<vector>
// 排列组合开始之前一定要先排序

using namespace std;

int main()
{
	vector<int> ivec;

	ivec.push_back(1);
	ivec.push_back(2);
	ivec.push_back(3);

	for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++)
		cout << *iter << ' ';
	cout << endl;
	//下一个排列组合 该算法返回值为bool  若返回值为true 说明还有下一个排列组合,但是若为fals 说明排列组合已经排列完了
	next_permutation(ivec.begin(), ivec.end());
	for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++)
		cout << *iter << ' ';
	cout << endl;
	
	while (next_permutation(ivec.begin(), ivec.end()))
	{
		for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++)
			cout << *iter << ' ';
		cout << endl;
	}
	
	cout << endl << endl;
	cout << endl << endl;
	cout << endl << endl;

	vector<int> ivec2;
	ivec2.push_back(3);
	ivec2.push_back(2);
	ivec2.push_back(1);
	for (vector<int>::iterator iter = ivec2.begin(); iter != ivec2.end(); iter++)
		cout << *iter; ' ';
	cout << endl;
	//ivec2 本身是个倒序
	while (prev_permutation(ivec2.begin(), ivec2.end()))
	{
		for (vector<int>::iterator iter = ivec2.begin(); iter != ivec2.end(); iter++)
			cout << *iter; ' ';
		cout << endl;
	}

	//
	system("pause");
	return 0;
}

技术分享



STL_算法(17)_排列组合 next_permutation() perv_permutation()

标签:

原文地址:http://blog.csdn.net/taotaoah/article/details/52227395

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!