GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::sample(3) C++ Standard Libary std::sample(3)

std::sample - std::sample


Defined in header <algorithm>
template< class PopulationIterator, class SampleIterator,


class Distance, class URBG >
SampleIterator sample( PopulationIterator first, PopulationIterator (since C++17)
last,
SampleIterator out, Distance n,


URBG&& g );


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 last-first
elements.


The algorithm is stable (preserves the relative order of the selected elements) only
if PopulationIterator meets the requirements of LegacyForwardIterator


The behavior is undefined if out is in [first; last).


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
n - number of samples to make
g - the random number generator used as the source of
randomness


-
PopulationIterator must meet the requirements of LegacyInputIterator.
-
SampleIterator must meet the requirements of LegacyOutputIterator.
-
SampleIterator must also meet the requirements of LegacyRandomAccessIterator if
PopulationIterator doesn't meet LegacyForwardIterator
-
PopulationIterator's value type must be writeable to out
-
Distance must be an integer type
-
std::remove_reference_t<URBG> must meet the requirements of
UniformRandomBitGenerator and its return type must be convertible to Distance


Returns a copy of out after the last sample that was output, that is, end of the
sample range.


Linear in std::distance(first,last).


This function may implement selection sampling or reservoir sampling.


Feature-test macro: __cpp_lib_sample


See the implementations in libstdc++, libc++ and MSVC STL.

// Run this code


#include <iostream>
#include <random>
#include <string>
#include <iterator>
#include <algorithm>


int main()
{
std::string in = "hgfedcba", out;
std::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';
}


five random letters out of hgfedcba: gfcba


random_shuffle
shuffle randomly re-orders elements in a range
(until C++17) (function template)
(C++11)
ranges::sample selects n random elements from a sequence
(C++20) (niebloid)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.