Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::sample

      From cppreference.com
      <cpp‎ |experimental
       
       
       
       
      Defined in header<experimental/algorithm>
      template<class PopulationIterator,class SampleIterator,

               class Distance,class URBG>
      SampleIterator sample( PopulationIterator first, PopulationIterator last,
                             SampleIterator out, Distance n,

                             URBG&& g);
      (1)(library fundamentals TS)
      template<class PopulationIterator,class SampleIterator,class Distance>

      SampleIterator sample( PopulationIterator first, PopulationIterator last,

                             SampleIterator out, Distance n);
      (2)(library fundamentals TS v2)

      Selectsn elements from the sequence[firstlast) 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.

      1) Random numbers are generated using the random number generatorg.
      2) Random numbers are generated using theper-thread engine.

      Contents

      [edit]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[firstlast)
      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.

      [edit]Return value

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

      [edit]Complexity

      Linear instd::distance(first, last).

      [edit]Notes

      This function may implement selection sampling or reservoir sampling.

      [edit]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

      [edit]See also

      (until C++17)(C++11)
      randomly re-orders elements in a range
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/sample&oldid=155685"

      [8]ページ先頭

      ©2009-2025 Movatter.jp