Suppose one wants to fill a vector with random numbers. Then there is a following obvious solution:
vector<int> result;
result.resize(n);
for (int i = 0; i < n; ++i) {
result[i] = generateRandomNumber();
}
OK, it obviously works, but I would like to understand what is the simplest STL/Boost way to get rid of the for loop. It is tempting to use std::transform, but it takes a function with one argument. Is there any nice STL way to introduce a dummy argument in a function?
The C++ standard library has
std::generate()andstd::generate_n();For example:
test: https://ideone.com/5xD6P
As for the second question, if I understand it correctly, is how to create a functor that takes an int argument, ignores it, and calls your
int f()?C++98 way is to actually write the whole functor:
test: https://ideone.com/DTsyl
C++11 way is to use a lambda expression
test: https://ideone.com/nAPXI
And the C++98/boost way is to use
boost::bindtest: https://ideone.com/cvd88