-1

C ++で次の問題を解決するのを誰かが手伝ってくれるかどうか疑問に思っています:

データが欠落しているファイルがあります。つまり、2 つの連続した TAB があり、たとえば 2 番目の 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 に答える