#include <iostream>
#include <random>
#include <algorithm>
int main()
{
std::mt19937 mt;
std::uniform_int_distribution<int> d(0, 255);
int l = 500;
std::vector<int> k(l);
std::generate(k.begin(), k.end(), d(mt));
return(0);
}
trying to compile with both g++ 4.7.2
and g++ 4.8.1
like so
g++ -std=c++11 main.cpp
the reported error is
In file included from /usr/include/c++/4.7/algorithm:63:0,
from main.cpp:3:
/usr/include/c++/4.7/bits/stl_algo.h: In instantiation of ‘void std::generate(_FIter, _FIter, _Generator) [with _FIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Generator = int]’:
main.cpp:11:42: required from here
/usr/include/c++/4.7/bits/stl_algo.h:5083:2: error: ‘__gen’ cannot be used as a function
I don't have any clue or idea about what can be possibly wrong in this case.
std::generate
takes a function object and applies its return value to the sequence container. But std::uniform_int_distribution<T>::operator ()
doesn't return a function object, it actually generates the random value and returns it. If you want to pass a functor, use a lambda:
std::generate(k.begin(), k.end(), [&] { return d(mt); });
// ^^^^^^^^^^^^^^^^^^^^^
Here is a working example of your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With