0

2列で行数が不明なテキストファイルがあります。したがって、2つの列を動的な2次元ベクトルにロードします。これが私がこれまでに持っているものです、それは機能していません。これが一次元の場合、私はこれを行う方法を知っています。空白は2つの列を区切ります。私は2つの列を読んでいるので、サイズnの2つのベクトルだけが必要です。

vector<vector<string> > component;
ifstream in_file("/tmp/FW.txt", ios::binary);

//Check if the file is open
if(!in_file.is_open()) 
{
   cout << "File not opened..." << endl;
   exit (1);
}

for(int i=0; !in_file.eof(); i++)
{
   in_file >> component.push_back();  
   //component.push_back(in_file);
}

誰かがこれを機能させる方法を教えてもらえますか?また、2次元ベクトルを元のファイルのように印刷する方法を教えていただければ、それもいいでしょう。これはLinux(Red Hat)で実行する必要があります

4

1 に答える 1

2

どうですか:

vector< vector<string> > component;
ifstream in_file("/tmp/FW.txt"); // N.B., not ios::binary since you're reading text strings
vector<string> vs( 2 );
// Assume they are separated by just whitespace
while( in_file >> vs[0] >> vs[1] )
{
    component.push_back( vs );
}
于 2012-04-13T20:23:29.427 に答える