2016-12-08 14 views
-2

多次元であるように、文字列のベクトルのベクトルを取り込みたいと思います。私は、次のベクトルがありますC++ debug:2次元ベクトル

[<start>, The <object> <verb> tonight.] 
[<object>, waves, big yellow flowers, slugs] 
[<verb>, sigh <adverb>, portend like <object>, die <adverb>] 
[<adverb>, warily, grumpily] 

をそして私は、このように、ベクターにそれらを追加したい:それは次のようになりますように

vector<vector<string>> vector2; 

vector2[0]: [<start>, The <object> <verb> tonight.] 
vector2[1]: [<object>, waves, big yellow flowers, slugs] 
vector2[2]: [<verb>, sigh <adverb>, portend like <object>, die <adverb>] 
vector2[3]: [<adverb>, warily, grumpily] 

はこれがあります私が持っているコード:

vector<vector<string>> vector2; 

    //the populated vector 1 is not shown here 
    for(int i = 0; i < vector1.size(); i++) 
    { 
     vector<string> vs = split_def(vector1[i]); 
     //calls a function that splits a string at index i of vector1 
     //by a certain character and stores the fragments in vector vs 

     cout << vs << endl; 
     //Note: I have an overloaded output operator function for vectors 

     /*for(int j = 0; j < vs.size(); j++) 
     { 
      vector2[i].push_back(vs[j]); 
      cout << vector2[i][j] << endl; 
     }*/ 

    } 

私のプログラムクラッシュを犯すことは、私が/ * * /の間にコメントした部分です。その前にすべてが動作します。私は自分の論理が正しいと仮定しています、それは間違った構文です。そのベクトルを埋め込むことが私に問題を与えているのです。誰でも私が間違っていることを知っている?

+1

'ベクトル2あなたがそれにアクセスしているとき、[i]は'初期化されていません。 –

+0

私は 'vector2 [i] [j] = vs [j];'を実行しようとしましたが、それでもクラッシュしました –

+0

私が最初に持っていたベクターはsplitDefsでしたが、私のコードではvector2です。私は一貫性のためにそれを変更しました(そしてまた単純化するために)、しかしそれらは同じベクトルです。私はそれを編集した。 –

答えて

0

それはあなたがベクトル2に作成しているベクトルを追加する必要があるように私には見えます:

vector<vector<string>> vector2; 

//the populated vector 1 is not shown here 
for(int i = 0; i < vector1.size(); i++) 
{ 
    vector<string> vs = split_def(vector1[i]); 
    //calls a function that splits a string at index i of vector1 
    //by a certain character and stores the fragments in vector vs 

    cout << vs << endl; 
    //Note: I have an overloaded output operator function for vectors 

    vector2.emplace_back(vs); 

}