指定调用std::copy的十进制精度
Specifying the decimal precision for a call to std::copy
我有以下函数将矢量保存为CSV文件:
#include <math.h>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>
using namespace std;
bool save_vector(vector<double>* pdata, size_t length,
const string& file_path)
{
ofstream os(file_path.c_str(), ios::binary | ios::out);
if (!os.is_open())
{
cout << "Failure!" << endl;
return false;
}
copy(pdata->begin(), pdata->end(), ostream_iterator<double>(os, ","));
os.close();
return true;
}
在生成的CSV文件中,pdata
中的数字以可变精度保存,并且没有一个以我想要的精度保存(小数点后10位)。
我知道函数std::setprecision
。但是,这个函数,根据文档
只能用作流操纵符。
(我实际上不确定我是否正确地解释了"流操纵器";我假设这意味着我不能在当前编写的函数中使用它。)
是否有一种方法可以让我指定使用copy
函数的十进制精度?如果不是,我应该如何摆脱copy
,以便我可以在上面的函数中使用setprecision
?
可以调用
os.precision(10);
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 在 c++ 中使用浮点数、双精度打印十进制
- 使用堆栈将字符串格式的十进制转换为双精度
- 您能否给我一个 16 位(或更多)十进制数,该十进制数仅在第 15 位正确转换为双精度浮点圆
- 十进制后,C 浮子将其串成非固定精度
- 将双精度转换为精度超过 5 位十进制数字的字符串
- 将浮点数转换为字符串,并指定精度和十进制数字数?
- 将带有千(和十进制)分隔符的字符串转换为双精度
- C++ 中的十进制精度错误
- 转换字符串十进制数时的精度
- 指定调用std::copy的十进制精度
- 固定的十进制精度
- 我失去了十进制GPS坐标的精度
- 以 32 个十进制数字分析浮点数/双精度
- c++如何为浮点数设置固定的十进制精度
- 如何在 C++ 中将双精度转换为 C# 十进制
- 如何计算出大双精度中有多少个十进制数字
- 双精度的有效十进制数