C++ 用c+将数据读入双数组+;
这是我的问题。我有一些改变维度的二维数据,我想读入一个二维的双精度数组。此外,在某些点上,文件中不是数字而是“NaN”,我想用零来代替。到目前为止,我的代码都能正常工作,但我只能读取整数。也许你能帮我把它读成双倍的 以下是我到目前为止得到的信息:C++ 用c+将数据读入双数组+;,c++,boost,datareader,C++,Boost,Datareader,这是我的问题。我有一些改变维度的二维数据,我想读入一个二维的双精度数组。此外,在某些点上,文件中不是数字而是“NaN”,我想用零来代替。到目前为止,我的代码都能正常工作,但我只能读取整数。也许你能帮我把它读成双倍的 以下是我到目前为止得到的信息: void READER(char filepath [], int target [129][128]) { //---------------------------- header double & int
void READER(char filepath [], int target [129][128])
{
//---------------------------- header double & int
int rowA = 0;
int colA = 0;
std::string line;
std::string x;
std::cout << "reading file: " << filepath << "\n";
std::cout << std::endl;
std::ifstream fileIN;
fileIN.open(filepath);
if (!fileIN.good())
std::cerr << "READING ERROR IN FILE: " << filepath << std::endl;
while (fileIN.good())
{
while (getline(fileIN, line))
{
std::istringstream streamA(line);
colA = 0;
while (streamA >> x)
{
boost::algorithm::replace_all(x, "NaN", "0");
boost::algorithm::replace_all(x, ",", ""); //. rein
// std::cout << string_to_int(x) << std::endl;
target [rowA][colA] = string_to_int(x);
colA++;
}
rowA++;
if(rowA%5 ==0)
{
std::cout << "*";
}
}
}
std::cout << " done." <<std::endl;
}
以下是一些示例数据:
“没错,这就是我对这行代码的想法,
boost::algorithm::replace_all(x,,,,,);
通过替换,通过替换。”
使用以下函数可转换为任何类型,例如double
:-
template <typename T>
T StringToNumber ( const std::string &Text )
{
std::istringstream ss(Text);
T result;
return ss >> result ? result : 0;
}
模板
字符串编号(常量标准::字符串和文本)
{
std::istringstream ss(文本);
T结果;
返回ss>>结果?结果:0;
}
电话使用:
boost::algorithm::replace_all(x, ",", "."); // Change , to .
std::cout << StringToNumber<double>(x) << std::endl;
boost::algorithm::replace_all(x,“,”,“);//换,换。
std::cout“没错,这就是我想用行boost::algorithm::replace_all(x,,,,,);
替换,替换。”
使用以下函数可转换为任何类型,例如double
:-
template <typename T>
T StringToNumber ( const std::string &Text )
{
std::istringstream ss(Text);
T result;
return ss >> result ? result : 0;
}
模板
字符串编号(常量标准::字符串和文本)
{
std::istringstream ss(文本);
T结果;
返回ss>>结果?结果:0;
}
电话使用:
boost::algorithm::replace_all(x, ",", "."); // Change , to .
std::cout << StringToNumber<double>(x) << std::endl;
boost::algorithm::replace_all(x,“,”,“);//换,换。
std::您能显示文件中的一些示例数据吗?您不能通过读取int
来读取double
。它们是不同的数据类型,具有非常不同的大小和位模式。您希望如何将它们读取为双精度,例如0.153 for 0153?确切地说,这就是我考虑使用boost::algorithm::replace_all(x,,,,,,);通过替换,通过。您可以显示文件中的一些示例数据吗?您不能通过读取int
来读取double
。它们是不同的数据类型,具有非常不同的大小和位模式。您希望如何将它们读取为双精度,例如0.153 for 0153?确切地说,这就是我考虑使用boost::algorithm::replace_all(x,,,,,,);通过替换,通过。