将std::normal_分布绑定到std::函数以存储在std::vector中 我试图将几种不同类型的C++分发存储到一个容器中,我认为我可以使用STD:函数来实现这个目的。我的尝试如下: void print_rand(){ // Make the random number generator std::random_device rd{}; std::mt19937 engine(rd()); // Store the distribution in a std::function auto n1 = std::make_shared<std::normal_distribution<double>>(0,1); std::function<double (std::mt19937)> fn = std::bind(&std::normal_distribution<double>::operator(), n1, std::placeholders::_1); // Call the function to get a normally distributed number std::cerr << fn(engine) << std::endl; } void print_rand(){ //制作随机数发生器 std::随机_设备rd{}; 标准:mt19937发动机(rd()); //将分发存储在std::函数中 自动n1=标准::使_共享(0,1); std::function fn=std::bind(&std::normal_distribution::operator(),n1,std::占位符::_1); //调用函数以获取正态分布的数字 std::cerr

将std::normal_分布绑定到std::函数以存储在std::vector中 我试图将几种不同类型的C++分发存储到一个容器中,我认为我可以使用STD:函数来实现这个目的。我的尝试如下: void print_rand(){ // Make the random number generator std::random_device rd{}; std::mt19937 engine(rd()); // Store the distribution in a std::function auto n1 = std::make_shared<std::normal_distribution<double>>(0,1); std::function<double (std::mt19937)> fn = std::bind(&std::normal_distribution<double>::operator(), n1, std::placeholders::_1); // Call the function to get a normally distributed number std::cerr << fn(engine) << std::endl; } void print_rand(){ //制作随机数发生器 std::随机_设备rd{}; 标准:mt19937发动机(rd()); //将分发存储在std::函数中 自动n1=标准::使_共享(0,1); std::function fn=std::bind(&std::normal_distribution::operator(),n1,std::占位符::_1); //调用函数以获取正态分布的数字 std::cerr,c++,c++11,templates,std,C++,C++11,Templates,Std,以下是如何将正态分布放入std::函数: std::normal_distribution<double> nd(0, 1); std::function<double(std::mt19937&)> fn = nd; std::正态分布nd(0,1); std::函数fn=nd; 无共享\u ptr、无绑定、无lambda、无随机数引擎复制(请注意&)。以下是如何将正态分布放入std::函数中的: std::normal_distribution<do

以下是如何将
正态分布
放入
std::函数

std::normal_distribution<double> nd(0, 1);
std::function<double(std::mt19937&)> fn = nd;
std::正态分布nd(0,1);
std::函数fn=nd;

共享\u ptr
、无
绑定
、无lambda、无随机数引擎复制(请注意
&
)。

以下是如何将
正态分布
放入
std::函数中的:

std::normal_distribution<double> nd(0, 1);
std::function<double(std::mt19937&)> fn = nd;
std::正态分布nd(0,1);
std::函数fn=nd;

共享\u ptr
,无
绑定
,无lambda,无随机数引擎复制(注意
).

谢谢,这要简单得多!将接受的答案更改为此。一个问题:std::function是否制作并保留发行版的副本?谢谢,这要简单得多!将接受的答案更改为此。一个问题:std::function是否制作并保留发行版的副本?
std::normal_distribution<double> nd(0, 1);
std::function<double(std::mt19937&)> fn = nd;