0

So I have a class called PCB:

class PCB
{
    private:

        int PID;
        string filename;
        int memStart;
        string cdrw;
        int filelength;

    public:

        PCB();
        PCB(int, string, int, string, int);
        virtual ~PCB();
        void getParam();
};

And I have a vector of queues: vector<queue<PCB>> printer;

How would I access the first element of the first queue in the vector? How would I use my class functions? Would it look like printer[0].getParam?

0

3 Answers 3

2

printer[0] gives you access to the first queue<PCB>.

printer[0].front() gives you access to the PCB at the front of the queue of the first queue<PCB>.

printer[0].front().getParam() allows you to call the getParam() function on the the PCB at the front of the queue of the first queue<PCB>.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man, I was thinking of that, but I wasn't super sure about how to write the syntax for that.
@TheCoxer, no problem. Glad to help.
1

A std::queue only provides facilities to access the first and last items directly using front() or back(). So if you want to call a function on one of those items from the vector then you would use

std::vector<std::queue<PCB>> printer;
// fill printer
printer[0].front().getParam();
// or
printer[0].back().getParam();

In short

printer[some_index].front()
// or
printer[some_index].back()

Returns a reference to the PCB in the container at that index.

4 Comments

What if I wanted to call two methods on the same object?
@bdbasinger then you would use printer[some_index].front().func1; and then printer[some_index].front().func2;.
Using .front() again wouldn’t cause it to move to the next item in the que after using it the first time?
@bdbasinger front only gives you a reference to the first element. It does not remove anything. To remove you need to use pop.
0

Here is a simple example using your code;

int main()
{
    vector<queue<PCB>> printer;

    // Create object your PCB class.
    PCB pcbObject;

    // Declare a queue
    queue<PCB> que;

    // Add the PCB class object to queue
    que.push(pcbObject);

    // Push the queue to vector.
    printer.push_back(que);

    //Get the first value
    printer[0].front().getParam();

    // Remove the element PCB
    printer[0].pop();

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.