std::ranges:: sample
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<algorithm>
|
||
|
呼び出しシグネチャ
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
std::
weakly_incrementable
O,
class
Gen
>
|
(1) | (C++20以降) |
|
template
<
ranges::
input_range
R,
std::
weakly_incrementable
O,
class
Gen
>
requires
(
ranges::
forward_range
<
R
>
||
std::
random_access_iterator
<
O
>
)
&&
|
(2) | (C++20以降) |
[
first
,
last
)
から
M
=
min
(
n, last
-
first
)
個の要素を(非復元抽出で)選択し、可能な各
サンプル
が等しい確率で現れるようにし、選択した要素を
out
から始まる範囲に書き込む。
[
first
,
last
)
の範囲内にある場合。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる) です。つまり:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定することはできません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つかった場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| first, last | - | サンプリングを行う要素の 範囲 を定義するイテレータ-センチネルペア( 母集団 ) |
| r | - | サンプリングを行う範囲( 母集団 ) |
| out | - | サンプルが書き込まれる出力イテレータ |
| n | - | 取得するサンプル数 |
| gen | - | ランダム性のソースとして使用される乱数生成器 |
戻り値
結果のサンプル範囲の終端、すなわち out + M に等しいイテレータ。
計算量
線形 : 𝓞 ( last - first ) .
注記
この関数は selection sampling または reservoir sampling を実装する可能性があります。
実装例
struct sample_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, class Gen> requires (std::forward_iterator<I> or std::random_access_iterator<O>) && std::indirectly_copyable<I, O> && std::uniform_random_bit_generator<std::remove_reference_t<Gen>> O operator()(I first, S last, O out, std::iter_difference_t<I> n, Gen&& gen) const { using diff_t = std::iter_difference_t<I>; using distrib_t = std::uniform_int_distribution<diff_t>; using param_t = typename distrib_t::param_type; distrib_t D{}; if constexpr (std::forward_iterator<I>) { // この分岐はサンプル要素の「安定性」を保持します auto rest{ranges::distance(first, last)}; for (n = ranges::min(n, rest); n != 0; ++first) if (D(gen, param_t(0, --rest)) < n) { *out++ = *first; --n; } return out; } else { // O は random_access_iterator です diff_t sample_size{}; // [first, first + M) の要素を「ランダムアクセス」出力にコピー for (; first != last && sample_size != n; ++first) out[sample_size++] = *first; // コピーされた要素の一部をランダムに選択された要素で上書き for (auto pop_size{sample_size}; first != last; ++first, ++pop_size) { const auto i{D(gen, param_t{0, pop_size})}; if (i < n) out[i] = *first; } return out + sample_size; } } template<ranges::input_range R, std::weakly_incrementable O, class Gen> requires (ranges::forward_range<R> or std::random_access_iterator<O>) && std::indirectly_copyable<ranges::iterator_t<R>, O> && std::uniform_random_bit_generator<std::remove_reference_t<Gen>> O operator()(R&& r, O out, ranges::range_difference_t<R> n, Gen&& gen) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(out), n, std::forward<Gen>(gen)); } }; inline constexpr sample_fn sample {}; |
例
#include <algorithm> #include <iomanip> #include <iostream> #include <iterator> #include <random> #include <vector> void print(auto const& rem, auto const& v) { std::cout << rem << " = [" << std::size(v) << "] { "; for (auto const& e : v) std::cout << e << ' '; std::cout << "}\n"; } int main() { const auto in = {1, 2, 3, 4, 5, 6}; print("in", in); std::vector<int> out; const int max = in.size() + 2; auto gen = std::mt19937{std::random_device{}()}; for (int n{}; n != max; ++n) { out.clear(); std::ranges::sample(in, std::back_inserter(out), n, gen); std::cout << "n = " << n; print(", out", out); } }
出力例:
in = [6] { 1 2 3 4 5 6 }
n = 0, out = [0] { }
n = 1, out = [1] { 5 }
n = 2, out = [2] { 4 5 }
n = 3, out = [3] { 2 3 5 }
n = 4, out = [4] { 2 4 5 6 }
n = 5, out = [5] { 1 2 3 5 6 }
n = 6, out = [6] { 1 2 3 4 5 6 }
n = 7, out = [6] { 1 2 3 4 5 6 }
関連項目
|
(C++20)
|
範囲内の要素をランダムに並べ替える
(アルゴリズム関数オブジェクト) |
|
(C++17)
|
シーケンスからN個のランダムな要素を選択する
(関数テンプレート) |