可以将元素洗牌,变得无序 注意要手动添加随机数种子,否则是伪随机
代码语言:javascript复制#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
//random_shuffle
void p(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v = { 1,2,3,4,5,6,7,8,9 };
cout << "未洗牌前:";
for_each(v.begin(), v.end(), p);
cout << "n洗牌后:";
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), p);
}
int main()
{
srand((size_t)time(NULL));
test01();
cout << endl;
system("pause");
return 0;
}