002random_shuffle指定范围内的元素随机调整次序

//002random_shuffle指定范围内的元素随机调整次序

/*
 *函数原型:
random_shuffle(iterator beg, iterator end);
// 指定范围内的元素随机调整次序
// beg 开始迭代器
// end 结束迭代器
 */
#include<iostream>
#include <set>
#include <functional>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
class MyPrint
{
public:
	void operator()(int val)
	{
		cout<<val<<" "; 
	}
};
void test01()
{
	srand((unsigned int)time(NULL));
	vector<int> v;
	for (int i=0;i<10;i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(),v.end(),MyPrint());
	cout<<endl;


	//打乱顺序
	random_shuffle(v.begin(),v.end());
	for_each(v.begin(),v.end(),MyPrint());
	cout<<endl;
}

int main(void)
{
	test01();
	system("pause");
	return 0;
}
/*
 * 总结:random_shuffle洗牌算法比较实用,使用时记得加随机数种子
 * 
 * 0 1 2 3 4 5 6 7 8 9
9 5 7 2 1 6 3 0 4 8
请按任意键继续. . .



 */

猜你喜欢

转载自blog.csdn.net/baixiaolong1993/article/details/89669362