Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass standard generators to STL functions?

Tags:

c++

stl

#include <random>
#include <algorithm>
#include <vector>

int main() {
    std::vector < int > a = {1, 2, 3};
    std::mt19937 generator;
    std::random_shuffle(a.begin(), a.end(), generator);
}

I am trying to compile this code with g++ -std=c++0x, receiving a huge compiler dump ending with

/usr/include/c++/4.9.2/bits/random.h:546:7: note:   candidate expects 0 arguments, 1 provided

Any way of doing it correctly?

like image 368
whoever Avatar asked Mar 17 '23 20:03

whoever


1 Answers

The overload of std::random_shuffle you're trying to use takes a "RandomFunc":

function object returning a randomly chosen value of type convertible to std::iterator_traits<RandomIt>::difference_type in the interval [0,n) if invoked as r(n)

In C++14, std::random_shuffle is deprecated in favour of std::shuffle, which takes a generator rather than that "RandomFunc". Simply change the function to std::shuffle.

like image 79
chris Avatar answered Apr 02 '23 20:04

chris