Std::shuffle-c++

Source: Internet
Author: User
Tags shuffle

In Python we often see Shuffle's random permutation function, which can randomly arrange the contents of a list, but in C + + you need to implement functions like this (c++0x). After c++0x, such function functions have a corresponding--std::shuffle in the standard library. The following is a specific description of the function: [CPP]View PlainCopy 
    1. Template <class Randomaccessiterator, class urng>
    2. void Shuffle (Randomaccessiterator first, Randomaccessiterator last, urng&& g);

function function: Use random generator g to randomly arrange the inner elements of an element [first, last] feasible exchange, presumably similar to the following code functions [CPP]View PlainCopy 
    1. template < Class randomaccessiterator, CLASS&NBSP;URNG>&NBSP;&NBSP;
    2. void shuffle  (randomaccessiterator first,  RANDOMACCESSITERATOR&NBSP;LAST,&NBSP;URNG&&&NBSP;G)   
    3. {   
    4.   for  (auto i =  (Last-first)  - 1;  i > 0; --i)  {  
    5. &NBSP;&NBSP;&NBSP;&NBSP;STD: : Uniform_int_distribution<decltype (i) > d  (0,i);   
    6.      swap  (first[i], first[d  (g)]);   
    7. &NBSP;&NBSP;}&NBSP;&NBSP;
    8. }  
The function must be used with the default random generator of the standard library, and the header file of the random generator is <random>. You can refer to the Random_shuffle function if you want to use a random generator function that does not need to be passed in.
Parameters:
First
, the beginning (begin) and end (end) of the last sequential container iterator, the values in the interval of [first, end] will be randomly sorted. The iterator for the sequential container must be the data type that defines the swap function and the order container must also support the element to be exchanged. An instance of the G unique random number generator, defined in header file <random>. URNGis the abbreviation for uniform random number generator.
return value:
None
Case: [CPP]View PlainCopy 
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>//Std::move_backward
  4. #include <random>/std::d efault_random_engine
  5. #include <chrono>//Std::chrono::system_clock
  6. int main (int argc, char* argv[])
  7. {
  8. std::vector<int> v;
  9. For (int i = 0; i < ++i) {
  10. V.push_back (i);
  11. }
  12. //Obtain a time-based seed:
  13. Unsigned seed = Std::chrono::system_clock::now (). Time_since_epoch (). Count ();
  14. Std::shuffle (V.begin (), V.end (), std::d efault_random_engine (Seed));
  15. For (auto& it:v) {
  16. Std::cout << It << "";
  17. }
  18. Std::cout << "\ n";
  19. return 0;
  20. }

Compile:
g++ main.cpp-o shuffle-std=c++0x
Execution output:
6 4 2 3 7 8 5 1 9
0
4 7 3 6 8 0 2 9 5
1

Std::shuffle-c++

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.