Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::sample

      From cppreference.com
      <cpp‎ |algorithm
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Defined in header<algorithm>
      template<class PopulationIt,class SampleIt,class Distance,class URBG>

      SampleIterator sample( PopulationIt first, PopulationIt last,

                             SampleIt out, Distance n, URBG&& g);
      (since C++17)

      Selectsn elements from the sequence[firstlast) (without replacement) such that each possible sample has equal probability of appearance, and writes those selected elements into the output iteratorout. Random numbers are generated using the random number generatorg.

      Ifn 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 ifPopulationIt meets the requirements ofLegacyForwardIterator.

      Ifthe value type offirst(until C++20)*first(since C++20) is notwritable toout, the program is ill-formed.

      If any of the following conditions is satisfied, the behavior is undefined:

      (until C++23)
      (since C++23)
      • The return type ofT is not convertible toDistance.
      (until C++20)

      Contents

      [edit]Parameters

      first, last - the pair of iterators defining therange of elements 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
      Type requirements
      -
      Distance must be an integer type.

      [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]Possible implementation

      See the implementations inlibstdc++,libc++ andMSVC STL.

      [edit]Notes

      This function may implement selection sampling orreservoir sampling.

      Feature-test macroValueStdFeature
      __cpp_lib_sample201603L(C++17)std::sample

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <random>#include <string> int main(){std::string in{"ABCDEFGHIJK"}, out;    std::sample(in.begin(), in.end(),std::back_inserter(out),4,std::mt19937{std::random_device{}()});std::cout<<"Four random letters out of "<< in<<" : "<< out<<'\n';}

      Possible output:

      Four random letters out of ABCDEFGHIJK: EFGK

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp