3

In C++98 I can use std::copy to copy from an input stream to a std::vector using iterator adapters. Is there some way to do a similar thing from the new C++11 random number library?

What I would like to be able to do is something like:

std::uniform_int_distribution<int> dist(0, 1024);
std::vector<int> col;
std::copy(std::adapter_type(dist), std::adapter_type(dist, 128), std::back_inserter(col));

Where the "end" iterator would have to have some way to limit how many numbers are inserted in the vector. And, yes, I do know that I need some type of engine to make the distribution work.

4

1 回答 1

8

You want generate_n (which is not actually new):

std::generate_n(std::back_inserter(col), 128, std::bind(dist, gen));

(Here gen is a suitable random number engine.)

于 2013-07-23T23:42:10.373 回答