Normal_distribution不是std的成员
normal_distribution is not a member of std
谁能告诉我如何才能摆脱这个错误?如果我需要生成正态分布。但都不适合我。
我试着用c++写代码。但显示:
错误1:错误C2039: 'mt19937':不是'std '的成员。
normal_distribution不是std
的成员
您忘了包含random
,它是定义mt19937 ("Mersenne Twister")生成器的头文件。
下面是一个完整的例子:
edd@max:/tmp$ cat cxx12_random.cpp
// use 'g++ -std=c++0x -o cxx12_random cxx12_random.cpp'
#include <random>
#include <iostream>
int main(int argc, char *argv[]) {
std::mt19937 engine(42);
std::normal_distribution<> normal(0.0, 1.0);
for (int i=0; i<5; i++) {
std::cout << normal(engine) << std::endl;
}
}
edd@max:/tmp$ g++ -std=c++0x -o cxx12_random cxx12_random.cpp
edd@max:/tmp$ ./cxx12_random
-0.550234
0.515433
0.473861
1.36845
-0.916827
edd@max:/tmp$
注意,我通过-std=c++0x
启用了较新的c++扩展。
我认为这只在c++11中可用。你的编译器支持它吗?
相关文章:
- std::ofstream 作为类成员删除复制构造函数?
- 使用 std::index_sequence 初始化具有固定大小数组成员的 POD 结构容器
- std::to_string - 'to_string' 不是 'std' 的成员 - Visual Studio Code 1.42.0
- 使用 std::async 时死锁,将来作为成员
- 访问 std:vector 的类成员 std:vector 在一个类中与另一个 std:vector
- 将此类的智能指针追加到成员 std::vector
- 在Visual Studio中删除成员std::vector::d ata()
- C++初始化/填充静态成员 std::数组元素到特定值
- 是否可以在同一对象中对成员 std::shared_ptr 进行成员引用<variable>?
- 成员std::future防止boost::shared_ptr超出范围
- 在GCC中使用Brace Initializer初始化成员STD ::数组时错误
- 错误:非静态引用成员"std::ostream&Student::out",无法使用默认赋值运算符
- 获取指向成员std::string::size的指针无法与libc++链接,但可以与libstdc++一起使用
- 如何创建静态常量成员 std::string 数组
- 如何在构造函数初始值设定项列表中初始化“std::unique_ptr”对象的类成员“std::vector”
- C++ 编译失败,并显示错误:命名空间中没有名为 'snprintf' 的成员'std'
- 重载类下标运算符以访问成员std::vector对象的元素
- 构造类成员 std::vector<std::unique_ptr > 的智能方法<AClass>
- 使用成员std::vector移动语义
- 初始化类成员std::函数时编译错误