STL 源代码剖析 算法 stl_algo.h -- random_shuffle
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
random_shuffle
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
描写叙述:将[first,last)的元素次序随机重排。
思路:
必须是 RandomAccessIterator
1.遍历区间
2.产生[0,i)中的一个随机数rand。将*(first+rand)与i交换
??怎么证明随机性
源代码:
template <class RandomAccessIterator>
inline void random_shuffle(RandomAccessIterator first,
RandomAccessIterator last) {
__random_shuffle(first, last, distance_type(first));
} template <class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator& rand) { //这里必须是传引用。 由于随机数产生器有局部状态。每次被调用都会改变
if (first == last) return;
for (RandomAccessIterator i = first + 1; i != last; ++i)
iter_swap(i, first + rand((i - first) + 1));
} template <class RandomAccessIterator, class Distance>
void __random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Distance*) {
if (first == last) return;
for (RandomAccessIterator i = first + 1; i != last; ++i)
#ifdef __STL_NO_DRAND48
iter_swap(i, first + Distance(rand() % ((i - first) + 1)));
#else
iter_swap(i, first + Distance(lrand48() % ((i - first) + 1)));
#endif
}
演示样例:
int main()
{
vector<int> vec;
for(int ix = 0; ix < 10; ix++)
vec.push_back(ix);
random_shuffle(vec.begin(), vec.end());
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
}
STL 源代码剖析 算法 stl_algo.h -- random_shuffle的更多相关文章
- STL 源代码剖析 算法 stl_algo.h -- search
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search --------------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partial_sort / partial_sort_copy ------------- ...
- STL 源代码剖析 算法 stl_algo.h -- lower_bound
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- merge sort
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- partition
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...
- STL 源代码剖析 算法 stl_algo.h -- equal_range
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie equal_range(应用于有序区间) ------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- inplace_merge
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- next_permutation
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie next_permutation ----------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- nth_element
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie nth_element ---------------------------------- ...
随机推荐
- Amzon MWS API开发之 上传数据
亚马逊上传数据,现有能操作的功能有很多:库存数量.跟踪号.价格.商品....... 我们可以设置FeedType值,根据需要,再上传对应的xml文件即可. 下面可以看看FeedType类型 这次我们拿 ...
- javascript闭包特性
http://www.cnblogs.com/rubylouvre/archive/2009/07/24/1530074.html var name = "The Window"; ...
- MFC 动态创建按钮
首先在对话框(模式对话框,无模式对话框)中添加一个ADD按钮,通过点击按钮产生的通告消息调用::OnBtnAdd()方法.此方法会在对话框的左上角创建一个按钮. 当然首先要在和次对话框相关联的类中添加 ...
- CURL常用命令---样例
原文地址: http://www.thegeekstuff.com/2012/04/curl-examples/ 下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://w ...
- error LNK1104: 无法打开文件“libboost_thread-vc140-mt-gd-1_61.lib”
error LNK1104: 无法打开文件“libboost_thread-vc140-mt-gd-1_61.lib” 调试->你的项目属性 配置属性->VC++目录 包含目录 D:\bo ...
- Ajax请求ashx返回各类数据的常见处理方式
.请求text数据,在success事件中手动解析 前台: $.ajax({ type: "post", url: "checkFile.ashx", data ...
- 素数回文(dfs,有bug)
素数回文 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- JQuery弹出窗口小插件ColorBox
本文来自: Small_陌 http://www.cnblogs.com/wggmqj/archive/2011/11/04/2236263.html 今天在博客园看到一篇<ASP.NET MV ...
- Linux 程序启停脚本
start.sh #!/bin/sh java -jar ./program.jar & echo $! > /var/run/program.pid stop.sh #!/bin/sh ...
- android 实现自己定义状态栏通知(Status Notification)
在android项目的开发中,有时为了实现和用户更好的交互,在通知栏这一小小的旮旯里,我们通常须要将内容丰富起来,这个时候我们就须要去实现自己定义的通知栏,比如以下360或者网易的样式: 首先我们要了 ...