C++ 向量的向量 push_back
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15838689/
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
vector of vectors push_back
提问by karl71
I'm designing a multilevel queue process simulator in C++ but I've got a problem when trying to implement several queues (my queues are vectors).So, "multilevel" is a 4 elements array (not vector). Inside each of those elements there is a vector (type t_PCB).
我正在用 C++ 设计一个多级队列过程模拟器,但是在尝试实现多个队列时遇到了问题(我的队列是向量)。所以,“多级”是一个 4 元素数组(不是向量)。在这些元素中的每一个内部都有一个向量(类型 t_PCB)。
vector<vector<t_PCB>> multilevel[4];
My question is: How can i insert an element at the end of one these 4 t_PCB
vectors? Thank you in advance.
我的问题是:如何在这 4 个t_PCB
向量之一的末尾插入一个元素?先感谢您。
I've tried the code line below but it doesn't work (error: not matching member function for call 'push_back')
我试过下面的代码行,但它不起作用(错误:不匹配调用'push_back'的成员函数)
multilevel[0].push_back(p); //where "p" is a t_PCB object
The line from above can not be used when talking about "multilevel" because this array only accepts arguments type: vector < t_PCB >
在谈论“多级”时不能使用上面的行,因为这个数组只接受参数类型:vector < t_PCB >
So, as I ask at the beginning: how can I push an object type "t_PCB" inside "multilevel"?
所以,正如我在开始时问的那样:如何将对象类型“t_PCB”推入“多级”中?
回答by Andy Prowl
By doing this:
通过做这个:
vector<vector<t_PCB> > multilevel[4];
You declare an arrayof four zero-sized vectors
, each of which can contain objects of type vector<t_PCB>
. What you probably wanted to do is rather:
您声明了一个由四个零大小组成的数组vectors
,每个数组都可以包含类型为 的对象vector<t_PCB>
。你可能想做的是:
vector<vector<t_PCB> > multilevel(4);
// ^^^
This will instantiate a vector of four default-initialized objects of type vector<t_PCB>
. Then, you can just do:
这将实例化一个由四个默认初始化的类型对象组成的向量vector<t_PCB>
。然后,你可以这样做:
multilevel[size].push_back(p);
Notice, though, that vector indices (like array indices) are zero-based, so size
must be less than the size of the vector.
但是请注意,向量索引(如数组索引)是从零开始的,因此size
必须小于向量的大小。
In the above expression, the sub-expression multilevel[size]
returns a reference to the size
-th vector inside multilevel
, and on that vector you are then invoking the member function push_back(p)
, which appends element p
to it.
在上面的表达式中,子表达式multilevel[size]
返回对size
内部第 -th 个向量的引用multilevel
,然后在该向量上调用成员函数push_back(p)
,该函数将元素附加p
到它。
回答by p.j
Declaring a two dimensional vector is similar to declaring an array. You can also use it in same way...
声明一个二维向量类似于声明一个数组。您也可以以相同的方式使用它...
vector<vector<int> > vec;
for(int i = 0; i < 5; i++)
{
vector<int> row;
vec.push_back(row);
}
vec[0].push_back(5);
cout << vec[0][0] << endl;
回答by Hugo Corrá
You are creating a array of vector<vector<t_PCB>>
instead of a single object.
您正在创建一个数组vector<vector<t_PCB>>
而不是单个对象。
I think the right way to do what you want is:
我认为做你想做的正确方法是:
vector<vector<t_PCB>> multilevel(4);
multilevel[0].push_back(p)
回答by 4pie0
回答by gitfredy
And just to put it out there, to access the vector of vectors:
只是把它放在那里,访问向量的向量:
multilevel[outer][inner]
where outer
will return the vector at that index and further indexing
with inner
will return the t_PCB
object. You could also replace the array-style indexing with the .at()function for bounds checks.
whereouter
将返回该索引处的向量,进一步索引inner
将返回该t_PCB
对象。您还可以使用.at()函数替换数组样式的索引以进行边界检查。
回答by Pawandeep Singh
vector<vector<int>> vec; // declare 2D vector
for (int i=0; i<=3; i++) {
vector<int> row; // create a row vector which adds a row to vec
for (int j=0; j<=4; j++) {
row.push_back(j*10); // push elements 0,10,20,30,40 to row
}
vec.push_back(row); // add this row to vec
// Repeat this procedure 4 times to make a 4*5 2D vector
}
cout<<"output is "<<vec[2][4]; // output is 40