C++STL算法篇next_permutation、prev_permutation将序列变成下一字典序算法

next_permutation(beg,end)和prev_permutation(beg,end)的特点

1:迭代器类型:双向迭代器
2:返回值类型:bool
3:算法功能:将当前序列改成下一字典序或上一字典序
4:复杂度:线性复杂度

#include
#include
#include
using namespace std;

int main()
{
	vector<int>coll = { 1,2,3 };
	copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
	cout << endl;

	next_permutation(coll.begin(),coll.end());

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	next_permutation(coll.begin(), coll.end());

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	next_permutation(coll.begin(), coll.end());

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	next_permutation(coll.begin(), coll.end());

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	next_permutation(coll.begin(), coll.end());

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	next_permutation(coll.begin(), coll.end());

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	
	prev_permutation(coll.begin(), coll.end());

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

}

你可能感兴趣的:(C++学习笔记)