std::experimental:: sample
|
ヘッダー
<experimental/algorithm>
で定義
|
||
|
template
<
class
PopulationIterator,
class
SampleIterator,
class
Distance,
class
URBG
>
|
(1) | (library fundamentals TS) |
|
template
<
class
PopulationIterator,
class
SampleIterator,
class
Distance
>
SampleIterator sample
(
PopulationIterator first, PopulationIterator last,
|
(2) | (library fundamentals TS v2) |
シーケンス
[
first
,
last
)
から
n
個の要素を選択し、各可能なサンプルが等しい確率で現れるようにし、選択された要素を出力イテレータ
out
に書き込みます。
n がシーケンス内の要素数を超える場合、 last - first 個の要素を選択します。
このアルゴリズムが安定するのは、
PopulationIterator
が
LegacyForwardIterator
の要件を満たす場合に限ります。
目次パラメータ
戻り値最後に出力されたサンプルの後、つまりサンプル範囲の終端にある out のコピーを返します。 計算量std:: distance ( first, last ) の要素数に対して線形。 注記この関数は選択サンプリングまたはリザーバーサンプリングを実装する可能性があります。 例
このコードを実行
#include <experimental/algorithm> #include <iostream> #include <iterator> #include <random> #include <string> int main() { std::string in = "abcdefgh", out; std::experimental::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'; } 出力例: five random letters out of abcdefgh : cdefg 関連項目
| |||||||||||||||||||||||||||||||||