我想填充字符串向量的向量,以便它是多维的。我有以下向量:
[<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;
}*/
}
我的程序崩溃的部分是我在/ * * /之间注释掉的部分。在此之前的一切工作。我假设我的逻辑是正确的,它只是语法错误。填充该向量是给我这个问题的原因。谁知道我做错了什么?
答案 0 :(得分:1)
在我看来,您需要将您正在创建的矢量添加到vector2:
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);
}