<分区>
我试图通过使用连续的“*”字符来反射(reflect)正态分布的条形来打印我的正态分布。该程序经常在打印之前崩溃,但有时会在打印之后崩溃。知道发生了什么事吗?
int main() {
std::normal_distribution<double> normDist(10.0, 4.0);
vector<int> v(20); // All zeros.
for (int i{}; i < 500; i++) {
int num = normDist(eng3);
if (num >= 0 && num < 20);
v[num]++;
}
for (int i{}; i < 20; i++) {
cout << i << ": " << std::string(v[i], '*') << endl;
}
}
结果:
0: *****
1: ***
2: *******
3: *******
4: ***********************
5: *********************
6: **********************************
7: **********************************
8: ********************************************
9: ********************************************
10: ****************************************************
11: **********************************************
12: *****************************************
13: **************************************************
14: ************************************
15: *******************
16: ************
17: ********
18: *******
19: **
double free or corruption (out)
Aborted (core dumped)