25

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"?

tshepang
  • 12,111
  • 21
  • 91
  • 136
karl71
  • 1,082
  • 2
  • 18
  • 29

6 Answers6

29

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.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • this code --> multilevel[size].push_back(p); only works if multilevel is declared as follows; vector multilevel[4]; – karl71 Apr 05 '13 at 18:49
  • @jafm92 Look carefully. You need to change the `[4]` to `(4)`. You were declaring an array of vectors of vectors. Andy is showing you how to declare just a vector of vectors. On the other hand, `vector multilevel[4]` is an array of vectors. You need to decide which one you want. – Joseph Mansfield Apr 05 '13 at 20:15
  • 1
    @jafm92 It seems from the way you're describing it that you want a vector of four vectors. In which case, do exactly as Andy has said. – Joseph Mansfield Apr 05 '13 at 20:17
  • @sftrabbit Thank You, i'm trying to implement a vector of 4 vectors. I thought that (4) was just a typing mistake. What is the difference between [4] and (4)? thank you (again) – karl71 Apr 05 '13 at 20:29
  • @jafm92 Please read this answer - he explains it fully. The `[4]` is declaring an *array* of vectors of vectors. So you have an array of four vectors of vectors. Instead you just want a vector of FOUR vectors. The `(4)` is passing an argument to the constructor of `std::vector`, telling it to initialise to 4 elements. **Alternatively**, a `vector multilevel[4]` would also work, which is an array of four vectors. – Joseph Mansfield Apr 05 '13 at 20:32
  • @jafm92: Sorry for the late answer. The difference is that `T x[4]` is used to create an array called `x` of 4 elements each of which has type T. The `T x(4)` constructs *one* object called `x` of type `T` and passes `4` as an argument to its constructor. Since, in your case, `T` is `vector>`, you are calling the constructor of `vector` which accepts its size as an argument, and by passing `4` you tell the constructor that you want your vector to contain 4 elements of type `vector`. Hope this clarifies – Andy Prowl Apr 05 '13 at 20:33
  • @sftrabbit: Thank you for trying to help, I was busy and could not answer myself - I appreciate it – Andy Prowl Apr 05 '13 at 20:33
  • @AndyProwl: I understand. My new problem is that when trying to declare like this (as a member variable of the class) "vector< vector > multilevel(4);" inside my class I get an error. – karl71 Apr 05 '13 at 20:44
  • 1
    @jafm92: OK, as a class member variable you cannot use that syntax. Either do what sftrabbit suggested (`vector multilevel[4]`), or just declare it as `vector< vector > multilevel;` and use constructor initialization lists to pass the value `4` to the constructor of `multilevel`. – Andy Prowl Apr 05 '13 at 20:49
21

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;
sg7
  • 6,108
  • 2
  • 32
  • 40
Parag
  • 662
  • 9
  • 15
5

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)
Hugo Corrá
  • 14,546
  • 3
  • 27
  • 39
2

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)
4pie0
  • 29,204
  • 9
  • 82
  • 118
0

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.

gitfredy
  • 399
  • 4
  • 3
-1
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
Pawandeep Singh
  • 830
  • 1
  • 13
  • 25