开发者

How do I simplify this templated vector initializer loop using lambdas or STL transform?

开发者 https://www.devze.com 2022-12-27 09:36 出处:网络
How do I simplify this templated vector initializer loop using lambdas or some kind of 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消