简体   繁体   English

在下面给出的示例中,我如何在向量的向量中 push_back?

[英]how can i push_back in a vector of vector in the example given below?

I am trying to parse a text file.我正在尝试解析文本文件。 The text file can be different it depends on the user.文本文件可以不同,这取决于用户。 The text file will be similar like this,文本文件将与此类似,

turn 1 12x13 14x14 5x4 1x2 

turn 2 13x13 14x14 5x3 1x3 3x5

turn 3 13x14 14x13 5x2 1x4 1x9 1x12 20x25

turn 4 14x14 14x12 5x1 1x5 12x9

turn 5 14x13 14x12 5x2 

I've created a vector<vector<string>> str to store the text line by line.我创建了一个vector<vector<string>> str来逐行存储文本。 But I want to store the text like this,但我想像这样存储文本,

str[0][0]=="12x13", str[0][0]=="14x14" ...
str[1][0]=="13x13", str[1][0]=="14x14" ...
str[2][0]=="13x14", str[1][0]=="14x13" ...
...

I want to store just the numbers separately, and ignore turn 1 , turn 2 etc.. How can I push_back these inputs in str?我只想单独存储数字,忽略turn 1轮、 turn 2等。我怎样才能在 str 中 push_back 这些输入?

You can initialize it like this:您可以像这样初始化它:

 vector<vector<string> > vec{ { "1", "2", "3" }, 
                          { "4", "5", "6" }, 
                          { "7", "8", "9" } };

or insert into the vector as:或插入向量中:

string s;    
for (int i = 0; i < ROW; i++) { 

        vector<string> v1; 

        for (int j = 0; j < COL; j++) { 
            cin>>s;
            v1.push_back(s); 
        } 
        vec.push_back(v1); 
    } 

If you have each line ready, you can push_back using如果你准备好每一行,你可以push_back使用

vector<vector<string>> str;
str.push_back({"12x13", "14x14", "5x4", "1x2"});

Or you can do this:或者你可以这样做:

vector<vector<string>> str;
str.push_back(vector<string>{});
str.at(0).push_back("12x13");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM