-1

我想知道是否有人可以帮助我在 C++ 中解决以下问题:

我有一个文件,其中有时缺少数据,即有两个连续的 TAB,然后我需要将第二个 TAB 转换为“-999999”或“0”。这是文件的样子

     i_1   i_2   i_3   i_4   i_5
j_1  12          14          16
j_2        11    17    25  
j_3  44                51    65

我想计算第一行元素的平均值,即(12,14 和 16)为:

sum+=tab[i][j];
mean = sum/5; (considering empty spaces =0)

谢谢你

4

1 回答 1

1
#include <boost/algorithm/string/split.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>

bool const compress_tokens = false;
bool const table_width = ...;

std::ifstream inp("filename");

// parsed grid
std::list<std::vector<std::string> > table;

std::string strbuf;
std::vector<std::string> vecbuf;
while(inp.getline(strbuf))
{
    vecbuf.clear();
    boost::split(vecbuf, strbuf, boost::is_any_of("\t"), compress_tokens);
    assert(vecbuf.size() == table_width);
    table.push_back(vecbuf);
}
于 2012-04-04T10:05:28.240 回答