C++ 二维向量 push_back

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16312682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:15:08  来源:igfitidea点击:

Two Dimensional Vector push_back

c++vector

提问by Barry Tormey

I have a two dimensional vector A made up of other vectors B

我有一个由其他向量 B 组成的二维向量 A

vector < vector < int >> A

vector < int > B

I use the push_backfunction to populate B.

我使用该push_back函数来填充 B。

B.push_back(1);
B.push_back(2);
B.push_back(3);

After that vector is populated, I use push_backagain to populate A with B

填充该向量后,我push_back再次使用 B 填充 A

A.push_back(B)

This is done several times so that A eventually results in a vector containing several other vectors looking like:

这样做多次,以便 A 最终产生一个包含其他几个向量的向量,如下所示:

A { {1 , 2 , 3 }, { 2, 2, 2 }, {8, 9, 10} }

How can I make a call to a specific index in A and then continue to add to the vector so that the output would be similar to

如何调用 A 中的特定索引,然后继续添加到向量中,以便输出类似于

A { {1 , 2 , 3 }, { 2, 2, 2, 4, 5, 6 }, {8, 9, 10} }

Something along the lines of

类似的东西

A[2].push_back(4);
A[2].push_back(5);
A[2].push_back(6);

采纳答案by Jesse Good

What you have is correct except that indexes start at 0, so it should be A[1].push_back(4);and not 2.

除了索引从 开始,你所拥有的都是正确的0,所以它应该是A[1].push_back(4);而不是2

回答by iambeanie

A[2].push_back(4);
A[2].push_back(5);
A[2].push_back(6);

Should work perfectly fine. Except if you want the second element then you'll need to use a[1] as vectors are 0 based.

应该工作得很好。除非您想要第二个元素,否则您需要使用 a[1] 因为向量是基于 0 的。