我正在尝试分析一个字符串行,该字符串行中用逗号分隔值并保留在Vector中。挑战在于我无法在特定位置后保留精度值。下面是我正在尝试的代码。
string name = "1, 0, -23.5346 -412.13647, 0, 11.2, 0.4, -2233.12345, 0, 0, 0, 567.46436, 0, 0, 1.654321, 0.1232"
stringstream ss(name);
vector<string> result;
int cc=0;
while (ss.good ()) {
string val;
getline (ss, val, ',');
cout <<"value for "<<cc<< " is "<<std::stod(val)<<endl;
result.push_back(val);
cout <<"cc "<< cc;
cc++;
}
我收到的输出是
value for 1 is 1
value for 2 is 0
value for 3 is -23.5346
value for 4 is -412.136
value for 5 is 0
value for 6 is 11.2
value for 7 is 0.4
value for 8 is -2233.12
value for 9 is 0
value for 10 is 0
value for 11 is 0
value for 12 is 567.464
value for 13 is 0
value for 14 is 0
value for 15 is 1.65432
value for 16 is 0.1232
在上面的代码中,我无法重新训练精度值,例如,存储值-412.136而不是-412.13647。我该如何解决这个问题?
答案 0 :(得分:0)
您的问题是std :: cout设置精度。
使用std :: fixed的this SO question解决方案。
double d = 3.14159265358979;
cout.precision(17);
cout << "Pi: " << fixed << d << endl;
答案 1 :(得分:0)
您是否尝试过setprecision?