| Technical Specification | ||||
| Filesystem library(filesystem TS) | ||||
| Library fundamentals(library fundamentals TS) | ||||
| Library fundamentals 2(library fundamentals TS v2) | ||||
| Library fundamentals 3(library fundamentals TS v3) | ||||
| Extensions for parallelism(parallelism TS) | ||||
| Extensions for parallelism 2(parallelism TS v2) | ||||
| Extensions for concurrency(concurrency TS) | ||||
| Extensions for concurrency 2(concurrency TS v2) | ||||
| Concepts(concepts TS) | ||||
| Ranges(ranges TS) | ||||
| Reflection(reflection TS) | ||||
| Mathematical special functions(special functions TR) | ||||
| Experimental Non-TS | ||||
| Pattern Matching | ||||
| Linear Algebra | ||||
| std::execution | ||||
| Contracts | ||||
| 2D Graphics |
experimental::sample | ||||
| Type-erased and polymorphic allocators | ||||
| Variable templates for type traits |
Defined in header <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) |
Selectsn elements from the sequence[first, last) such that each possible sample has equal probability of appearance, and writes those selected elements into the output iteratorout.
Ifn is greater than the number of elements in the sequence, selectslast- first elements.
The algorithm is stable only ifPopulationIterator meets the requirements ofLegacyForwardIterator.
Contents |
| 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 range[first, last) |
| n | - | number of samples to make |
| g | - | the random number generator used as the source of randomness |
-PopulationIterator must meet the requirements ofLegacyInputIterator. | ||
-SampleIterator must meet the requirements ofLegacyOutputIterator. | ||
-SampleIterator must also meet the requirements ofLegacyRandomAccessIterator ifPopulationIterator doesn't meetLegacyForwardIterator. | ||
-PopulationIterator's value type must be writeable toout. | ||
-Distance must be an integer type. | ||
-URBG must meet the requirements ofUniformRandomBitGenerator and its return type must be convertible toDistance. | ||
Returns a copy ofout after the last sample that was output, that is, end of the sample range.
Linear instd::distance(first, last).
This function may implement selection sampling or reservoir sampling.
#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';}
Possible output:
five random letters out of abcdefgh : cdefg
(until C++17)(C++11) | randomly re-orders elements in a range (function template)[edit] |