Sil*_*Net 4 c++ random class tr1
我正在使用VC 2010并试图通过将随机定义放在每个类实例的构造函数中来保持某些函数的开销和重复代码,然后根据需要从那里调用.我现在简化的是:
#include <random>
#include <Windows.h>
mt19937 eng(GetTickCount());
class Cycles {
int line;
normal_distribution<> rand_norm;
variate_generator<mt19937&,normal_distribution<>> r_norm;
public:
Cycles()
: rand_norm(0.85,0.05),
r_norm(eng,rand_norm) {
line=0;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用,我最终得到了这个错误:
\ vc\include\random(513):错误C2248:'std :: tr1 :: _ Ewrap <_Engine,_Tgt_type> :: operator =':无法访问类'std :: tr1 :: _ Ewrap <_Engine中声明的私有成员, _Tgt_type>"
\ vc\include\random(446):查看'std :: tr1 :: _ Ewrap <_Engine,_Tgt_type> :: operator ='的声明
此诊断发生在编译器生成的函数'std :: tr1 :: variate_generator <_Engine,_Distrib>&std :: tr1 :: variate_generator <_Engine,_Distrib> :: operator =(const std :: tr1 :: variate_generator <_Engine,_Distrib >&)'
我理解这些应该在构造函数打开之前初始化,否则由于缺少默认构造函数而导致错误,但我不明白为什么会失败.我的C++ fu很生疏.
我看到的每个例子都显示分布器和生成器在调用它的函数中全局或本地初始化,这对我来说似乎很愚蠢,因为我有几个成员函数将使用在紧密循环中调用的r_norm.它严重失败了气味测试.没有人知道我错过了什么吗?
bind()用于c ++ 0x而不是variate_generator<>:
#include <functional> // bind
#include <iostream>
#include <vector>
#include <random>
namespace {
std::random_device generate_seed;
std::mt19937 eng(generate_seed());
class Cycles {
int line;
std::normal_distribution<> rand_norm;
std::function<double()> r_norm;
public:
Cycles(): line(0), rand_norm(0.85, 0.05), r_norm(std::bind(rand_norm, eng))
{
// print distribution
std::vector<size_t> freq(200);
for (int i = 0; i < 900; ++i) ++freq.at(std::round(r_norm()*100));
size_t line = 0, max_line = freq.size()-1;
for ( ; not freq[line] and line <= max_line; ++line);
for ( ; not freq[max_line] and max_line >= line; --max_line);
for ( ; line <= max_line; ++line) {
std::cout << line << '\t';
for (size_t j = 0; j < freq[line]; ++j) std::cout << '*';
std::cout << std::endl;
}
}
};
}
int main() {
Cycles c;
}
Run Code Online (Sandbox Code Playgroud)
该剧情的灵感来自C++ 0xFAQ.
72 **
73 ***
74 ******
75 ********
76 *****************
77 ********************
78 *******************************
79 ************************************************
80 *************************************************
81 **********************************************************
82 ************************************************************
83 ***********************************************************
84 ************************************************************************
85 ********************************************************************************
86 *********************************************************************
87 ************************************************************
88 ****************************************************
89 *************************************
90 **********************************
91 **************************************
92 *************************
93 ******************
94 ********************
95 ************
96 ****
97 **********
98 ***
99 **
100 **
101 *
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1804 次 |
| 最近记录: |