std::ranges::sample
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
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 起) |
1) 從序列
[
first,
last)
中選擇 M = min(n, last - first) 個元素(不替換),使得每個可能的樣本出現的機率相等,並將這些選定的元素寫入以 out 開頭的範圍中。 當且僅當 `I` 建模 std::forward_iterator 時,該演算法是穩定的(保留選定元素的相對順序)。
如果 out 在
[
first,
last)
範圍內,則行為未定義。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要進行取樣的元素範圍(總體)的迭代器-哨兵對 |
r | - | 要進行取樣的範圍(總體) |
out | - | 寫入樣本的輸出迭代器 |
n | - | 要取樣的數量 |
gen | - | 用作隨機性來源的隨機數生成器 |
[編輯] 返回值
一個等於 out + M 的迭代器,即結果樣本範圍的末尾。
[編輯] 複雜度
線性:𝓞(last - first)。
[編輯] 注意
此函式可以實現選擇取樣或水塘抽樣。
[編輯] 可能的實現
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>) { // this branch preserves "stability" of the sample elements 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 is a random_access_iterator diff_t sample_size{}; // copy [first, first + M) elements to "random access" output for (; first != last && sample_size != n; ++first) out[sample_size++] = *first; // overwrite some of the copied elements with randomly selected ones 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 個隨機元素 (函式模板) |