std::experimental::sample(3)
π NAME π
std::experimental::sample β std::experimental::sample
okay bestie, welcome to std::experimental::sample(3) from the C++ Standard Libary manual (yes, the header spells it βLibaryβ π), and this one is lowkey iconic β¨
β¨ Synopsis β¨
Defined in header <experimental/algorithm> β that's the vibe check, no cap π«Ά
template< class PopulationIterator, class SampleIterator,
class Distance, class URBG >
SampleIterator sample( PopulationIterator first, (1) (library fundamentals TS)
PopulationIterator last,
SampleIterator out, Distance n,
URBG&& g );
template< class PopulationIterator, class SampleIterator, class Distance >
SampleIterator sample( PopulationIterator first, (2) (library fundamentals TS v2)
PopulationIterator last,
SampleIterator out, Distance n
);
Selects n elements from the sequence [first, last) such that each possible sample has equal probability of appearance, and writes those selected elements into the output iterator out. Slay, that's the whole assignment π
If n is greater than the number of elements in the sequence, selects last - first elements. fr fr, it caps itself.
The algorithm is stable only if PopulationIterator meets the requirements of LegacyForwardIterator.
- Random numbers are generated using the random number generator
g. - Random numbers are generated using the per-thread engine.
π«Ά Parameters π«Ά
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.
bestie warning:outmust not be in the range[first, last)β that's a hard no π ββοΈ
PopulationIteratormust meet the requirements ofLegacyInputIterator.SampleIteratormust meet the requirements ofLegacyOutputIterator.SampleIteratormust also meet the requirements ofLegacyRandomAccessIteratorifPopulationIteratordoesn't meetLegacyForwardIterator.PopulationIterator's value type must be writeable toout.Distancemust be an integer type.URBGmust meet the requirements ofUniformRandomBitGeneratorand its return type must be convertible toDistance.
π Return value π
Returns a copy of out after the last sample that was output, that is, end of the sample range. So you get the after-glow iterator, periodt β¨
π₯ Complexity π₯
Linear in std::distance(first, last).
π§ Notes π§
This function may implement selection sampling or reservoir sampling. iykyk π§
π Example π
// Run this code
#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
π See also π
random_shuffleβ(until C++17)(function template)shuffleβ randomly re-orders elements in a range(C++11)(function template)
π Category π
- Noindexed pages
later bestie! this page came from http://cppreference.com, version/date 2024.06.10, and it's literally std::experimental::sample(3) π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms