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).
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.
I've tried the code line below but it doesn't work ( error : not matching member function for call '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 >
So, as I ask at the beginning: how can I push an object type "t_PCB" inside "multilevel"?
By doing this:
vector<vector<t_PCB> > multilevel[4];
You declare an array of four zero-sized vectors
, each of which can contain objects of type vector<t_PCB>
. What you probably wanted to do is rather:
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:
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.
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.
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;
You are creating a array of vector<vector<t_PCB>>
instead of a single object.
I think the right way to do what you want is:
vector<vector<t_PCB>> multilevel(4);
multilevel[0].push_back(p)
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_back at the end of the vector indexed with WHICH
this way:
multilevel[WHICH].push_back(p)
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.