首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::sample

Defined in header <algorithm>

?

?

template< class PopulationIterator, class SampleIterator, class Distance, class UniformRandomBitGenerator > SampleIterator sample( PopulationIterator first, PopulationIterator last, SampleIterator out, Distance n, UniformRandomBitGenerator&& g);

?

(since C++17)

n序列中的元素。[首先,最后%29,这样每个可能的样本都有相同的出现概率,并将这些选定的元素写入输出迭代器。out使用随机数生成器生成随机数。g...

如果n大于序列中的元素数,则选择last-first元素。

该算法只有在以下情况下才是稳定的PopulationIterator满足…的要求ForwardIterator...

参数

first, last

-

pair of iterators forming the range from which to make the sampling (the population)

out

-

the output iterator where the samples are written. Must not be in the [first;last) range

n

-

number of samples to make

g

-

the random number generator used as the source of randomness

-用户必须符合输入器的要求。

-取样器必须符合输出器的要求。

-如果迭代器%27T满足前驱器,采样器也必须满足RandomAccessIterator的要求。

-PopulationIterator%27s值类型必须是可写的

-距离必须是整数类型

-性病::清除[医]参照系[医]T型<UniformRandomBitGenerator>必须满足UniformRandomBitGenerator的要求,其返回类型必须转换为距离

返回值

返回out在输出的最后一个示例之后,即样本范围的结束。

复杂性

线性在std::distance(first,last)...

注记

该功能可以实现选择采样或储层采样。

二次

代码语言:javascript
复制
#include <iostream>
#include <random>
#include <string>
#include <iterator>
#include <algorithm>
 
int main()
{
    std::string in = "abcdefgh", out;
    std::sample(in.begin(), in.end(), std::back_inserter(out),
                5, std::mt19937{std::random_device{}()});
    std::cout << "five random letters out of " << in << " : " << out << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
five random letters out of abcdefgh : cdefg

二次

另见

random_shuffleshuffle (until C++17)(C++11)

randomly re-orders elements in a range (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com