SampleIterator sample( PopulationIt first, PopulationIt last,
Selects n elements from the sequence [
first,
last)
(without replacement) such that each possible sample has equal probability of appearance, and writes those selected elements into the output iterator out. Random numbers are generated using the random number generator g.
If n is greater than the number of elements in the sequence, selects all elements in the sequence.
The algorithm is stable (preserves the relative order of the selected elements) only if PopulationIt
meets the requirements of LegacyForwardIterator.
If the value type of first(until C++20)*first(since C++20) is not writable to out, the program is ill-formed.
If any of the following conditions is satisfied, the behavior is undefined:
[
first,
last)
.PopulationIt
does not meet the requirements of LegacyInputIterator.SampleIt
does not meet the requirements of LegacyOutputIterator.SampleIt
does not meet the requirements of LegacyRandomAccessIterator.T
as std::remove_reference_t<URBG>, any of the following conditions is satisfied:T
does not meet the requirements of UniformRandomBitGenerator.T
is not convertible to Distance
.Distance
must be an integer type. [edit] Return value
Returns a copy of out after the last sample that was output, that is, end of the sample range.
[edit] ComplexityLinear in std::distance(first, last).
[edit] Possible implementationSee the implementations in libstdc++, libc++ and MSVC STL.
[edit] NotesThis function may implement selection sampling or reservoir sampling.
[edit] ExamplePossible output:
Four random letters out of ABCDEFGHIJK: EFGK[edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4