我以前从未使用过boost。我下载了许多其他库附带的 MinGW 包,其中包括 boost。我有旧版本的 g++,想使用 boost 编译来自该站点的代码:http://en.cppreference.com/w/cpp/numeric/随机
避免使用类似的东西:std::srand ( unsigned ( std::time(0) ) );
我想开始积极使用 C++11 功能,但首先我想在这种情况下通过使用 boost 来实现这一点(稍后我将下载 4.6.x)代码如下:
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <cmath>
int main()
{
// Seed with a real random value, if available
std::random_device rd;
// Choose a random mean between 1 and 6
std::default_random_engine e1(rd());
std::uniform_int_distribution<int> uniform_dist(1, 6);
int mean = uniform_dist(e1);
std::cout << "Randomly-chosen mean: " << mean << '\n';
// Generate a normal distribution around that mean
std::mt19937 e2(rd());
std::normal_distribution<> normal_dist(mean, 2);
std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
++hist[std::round(normal_dist(e2))];
}
std::cout << "Normal distribution around " << mean << ":\n";
for (auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/200, '*') << '\n';
}
}
ideone 编译它。但是使用 g++ main.cpp -std=c++0x -I c:\MinGW\include\boost\ 我得到错误:
PS C:\Users\localhost\Desktop\C++\snippets> g++ randomBoost.cpp -std=c++0x -I c:\MinGW\include\boost\
randomBoost.cpp: In function 'int main()':
randomBoost.cpp:29:17: error: expected initializer before ':' token
randomBoost.cpp:33:1: error: expected primary-expression before '}' token
randomBoost.cpp:33:1: error: expected ';' before '}' token
randomBoost.cpp:33:1: error: expected primary-expression before '}' token
randomBoost.cpp:33:1: error: expected ')' before '}' token
randomBoost.cpp:33:1: error: expected primary-expression before '}' token
randomBoost.cpp:33:1: error: expected ';' before '}' token
谢谢你。