How do I simplify this templated vector initializer loop using lambdas or STL transform?
How do I simplify this templated vector initializer loop using lambdas or some kind of STL transform?
template<typename T>
template<typename... Args>
void InitToRandomValues(vector<T>* retval, int n, RNG& rng, Args const&... args) {
retval->resize(n);
for (auto it = retval->begin(); it != retval->end(); ++it) {
typename T::CPDDist cpd(rng, args.开发者_Go百科..);
*it = T(cpd);
}
}
I think this would work:
template<typename T>
template<typename... Args>
void InitToRandomValues(vector<T>* retval, int n, RNG& rng, Args const&... args) {
retval->resize(n);
std::generate(retval->begin(), retval->end(), [] -> T() {
typename T::CPDDist cpd(rng, args...);
return T(cpd); });
}
It isn't much of a simplification though.
精彩评论