如何推回向量的向量? [英] how to pushback on a vector of vectors?

查看:115
本文介绍了如何推回向量的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行20行输入。我想用一个空格分隔每行的内容,并将其放入向量的向量。如何做一个向量的向量?

 <$> 

c $ c>玛丽有一只小羊
lalala山上
太阳升起

向量应该看起来像这样。

  ROW 0:{Mary,had a,little,lamb} 
ROW 1:{lalala,up,the,hill}

这是我的代码....

  
vector< vector< string> >大;
string buf;
for(int i = 0; i< 20; i ++){
getline(cin,line);
stringstream ss(line);

while(ss>> buf){
(big [i])。push_back(buf);
}
}


解决方案

代码是正确的,但是你的向量中有零个元素,所以你不能访问 big [i]



循环之前的向量大小,在构造函数中,或者像这样:

  big.resize(ruleNum); 

或者,您可以在每个循环步骤中推一个空向量:

  big.push_back(vector< string>()); 

您不需要 big [i]


I am taking 20 lines of input. I want to separate the contents of each line by a space and put it into a vector of vectors. How do I make a vector of vectors? I am having have struggles pushing it back...

My input file:

Mary had a little lamb
lalala up the hill
the sun is up

The vector should look like something like this.

ROW 0: {"Mary","had", "a","little","lamb"}
ROW 1: {"lalala","up","the","hill"}

This is my code....

string line; 
vector <vector<string> > big;
string buf;
for (int i = 0; i < 20; i++){
    getline(cin, line);
    stringstream ss(line);

    while (ss >> buf){
        (big[i]).push_back(buf);
    }
}

解决方案

The code is right, but your vector has zero elements in it so you cannot access big[i].

Set the vector size before the loop, either in the constructor or like this:

big.resize(ruleNum);

Alternatively you can push an empty vector in each loop step:

big.push_back( vector<string>() );

You don't need the parentheses around big[i] either.

这篇关于如何推回向量的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆