📄 random_sample.cpp
字号:
// random_sample.cpp : 定义控制台应用程序的入口点。
// [2009/5/3/16:58 wxf]
//////////////////////////////////////////////////////////////////////////
// 随机采样算法对区间元素进行随机采样,有如下两个使用原型,将迭代器区间
// [first,last)元素,用C标准伪随机函数或自定义的rand函数对象,抽取元素到
// 区间[ofirst,olast)。其中,带有采样个数n的random_sample函数内部,首先将
// 输出区间填满n个元素,这里n一般小于被采样区间的元素个数;然后再依次将
// [first,last)区间的元素写入out区间,每次写的out位置,由一个随机数决定,
// 这样,[first,last)区间的元素不会重复被采样到out区间。
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(int x){
cout<<x<<" ";
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> v(10);
for(unsigned int i = 0; i != v.size(); ++i)
v[i] = i;
cout<<"v=";
for_each(v.begin(),v.end(),print);
cout<<endl;
const int n = 6;
int iArray[n];
random_sample(v.begin(),v.end(),iArray,iArray+n);
cout<<n<<"个采样元素为";
for_each(iArray,iArray+n,print);
cout<<endl;
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -