方法一:STL的random_shuffle算法(因为如果一次查找是否与以前的相等,太浪费时间,不如用一个队列用到一个,删一个。节省时间)
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
int num;
cout < < "Please input the num: ";
cin > > num;
vector <int> v;
for (int i = 0; i < num; ++i)
{
v.push_back(i);
}
//显示
for (unsigned int i = 0; i < v.size(); ++i)
{
cout < < v[i] < < " ";
}
cout < < endl;
//随机排序
srand((unsigned)time(NULL)); //+ 增加这一行,以时间作为随机数种子
random_shuffle(v.begin(), v.end());
//显示
for (unsigned int i = 0; i < v.size(); ++i)
{
cout < < v[i] < < " ";
}
cout < < endl;
}