#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
//随机重排与抽样
int main()
{
/************************************************************************/
//random_shuffle
/************************************************************************/
//random_shuffle:重新对[first,last)进行排序,共有(last-first)!种全排列,函数随机抽取一个作为结果
//版本1使用内部随机数产生随机顺序,每次运行的结果都一样
//版本2使用外部随机数对象,所以产生的结果不一样
/*
template<class RandomAccessIterator>
void random_shuffle(
RandomAccessIterator _First,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(
RandomAccessIterator _First,
RandomAccessIterator _Last,
RandomNumberGenerator& _Rand
);
*/
std::vector<int> iv;
generate_n(std::back_inserter(iv), 5, []{return rand() % 10;});
std::random_shuffle(iv.begin(), iv.end());
std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
return 0;
}
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | |