gpt4 book ai didi

C++ 模板函数 - 多种类型、默认值和...参数?

转载 作者:行者123 更新时间:2023-11-30 03:18:55 33 4
gpt4 key购买 nike

我有几个样板函数,我想用模板替换。它们看起来大致像:

std::vector<double> generate_means(
std::mt19937& g, unsigned int N,
double lower = -1.0, double upper = 1.0
) {
std::uniform_real_distribution<double> dist(lower, upper);
std::function<double()> rng = std::bind(dist, g);
std::vector<double> res(N);
std::generate(std::begin(res), std::end(res), gen);
return res;
}

需要抽象的元素是返回类型(只包含类型,总是vector就可以)N之后的参数(例如,lowerupper 在这种情况下)和分布(例如,std::uniform_real_distribution)。

我大致希望能够写的内容:

auto generate_means = generate_template<
double, // results vector<double>
std::uniform_real_distribution, // uses uniform distro
double=-1.0,double=1.0 // with default args
>
auto generate_norm_deviates = generate_template<
double, // still provides vector<double>
std::normal_distribution, // different distro
double=0, double=1.0 // different defaults
>
auto generate_category_ids = generate_template<
unsigned int,
std::uniform_int_distribution,
unsigned int=0, unsigned int // again with two args, but only one default
>

我有一些子片段

template <class NUMERIC>
using generator = std::function<NUMERIC()>;

template <class NUMERIC>
std::vector<NUMERIC> series(unsigned int length, generator<NUMERIC> gen) {
std::vector<NUMERIC> res(length);
std::generate(std::begin(res), std::end(res), gen);
return res;
};

但是当我尝试组装时,例如

template <class NUMERIC, class DIST, class...Args>
std::vector<NUMERIC> generator_template(
std::mt19937& g, unsigned int N,
Args... args
) {
DIST<NUMERIC> dist(&args...);
generator<NUMERIC> gen = std::bind(dist, g);
return series(N, gen);
}

我遇到了编译错误(在本例中为 error: expected unqualified-id)。我想要的东西大致可以实现吗?这种方法是正确的方向,还是我需要做一些根本不同的事情?如果方向正确,我错过了什么?

编辑:

对于应用程序约束:我希望能够声明具有参数默认值的生成器,但我确实需要偶尔在没有默认值的情况下使用它们。没有默认值只是不方便,但不是致命的。示例:

//... assorted calculations...
auto xmeans = generate_means(rng, 100); // x on (-1,1);
auto ymeans = generate_means(rng, 100); // y on (-1,1);
auto zmeans = generate_means(rng, 100, 0, 1); // z on (0,1);

最佳答案

不可能将 float 作为模板参数。但是,您可以执行以下操作:

#include <random>
#include <limits>
#include <algorithm>
#include <vector>
#include <iostream>

template<typename T, template<typename> typename Distribution>
auto generate_random_template(T min = std::numeric_limits<T>::lowest(),
T max = std::numeric_limits<T>::max()) {
return [distribution = Distribution<double>{min, max}]
(auto &&generator, std::size_t number) mutable {
std::vector<T> result;
result.reserve(number);
auto generate = [&](){return distribution(generator);};
std::generate_n(std::back_inserter(result), number, generate);
return result;
};
}

int main() {
auto generate_means = generate_random_template<double, std::uniform_real_distribution>(0.0, 1.0);
std::mt19937 g;
std::vector<double> randoms = generate_means(g, 10);

for(auto r : randoms) std::cout << r << std::endl;

return 0;
}

编辑:出于性能原因,使用 generate_n 而不是 generate

EDIT2:如果你想像对 x、y 和 z 那样使用默认参数,你也可以这样做:

#include <random>
#include <limits>
#include <algorithm>
#include <vector>
#include <iostream>

template<typename T, template<typename> typename Distribution>
auto generate_random_template(T min = std::numeric_limits<T>::lowest(),
T max = std::numeric_limits<T>::max()) {
return [distribution = Distribution<double>{min, max}, min, max]
(auto &&generator, std::size_t number, auto ...args) mutable {
std::vector<T> result;
result.reserve(number);

if constexpr(sizeof...(args) > 0)
distribution.param(typename Distribution<T>::param_type(args...));

else
distribution.param(typename Distribution<T>::param_type(min, max));

auto generate = [&](){return distribution(generator);};
std::generate_n(std::back_inserter(result), number, generate);
return result;
};
}

int main() {
auto generate_means = generate_random_template<double, std::uniform_real_distribution>(-1.0, 1.0);
std::mt19937 g;
// x and y are between -1 and 1
std::vector<double> x = generate_means(g, 10);
std::vector<double> y = generate_means(g, 10);
std::vector<double> z = generate_means(g, 10, 0.0, 1.0); // z is between 0 and 1

for(int i = 0; i < 10; ++i) {
std::cout << x[i] << "," << y[i] << "," << z[i] << std::endl;
}
return 0;
}

关于C++ 模板函数 - 多种类型、默认值和...参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54216484/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com