Normal_distribution不是std的成员

normal_distribution is not a member of std

本文关键字:成员 std 不是 distribution Normal      更新时间:2023-10-16

谁能告诉我如何才能摆脱这个错误?如果我需要生成正态分布。但都不适合我。

我试着用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中可用。你的编译器支持它吗?