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

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

vector of vectors push_back

c++vectorprocesspush-backmulti-level

提问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_PCBvectors? 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 sizemust 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 pto 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

You can create a vector instead of array:

您可以创建一个向量而不是数组:

std::vector< std::vector<t_PCB>> multilevel(4); // 2 dim array, 1st dim is 4

and then you can push_backat the end of the vector indexed with WHICHthis way:

然后你可以在用这种方式索引的向量的末尾push_backWHICH

multilevel[WHICH].push_back(p)

回答by gitfredy

And just to put it out there, to access the vector of vectors:

只是把它放在那里,访问向量的向量:

multilevel[outer][inner]

where outerwill return the vector at that index and further indexing with innerwill return the t_PCBobject. 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