1

I currently have a queue that holds a user specified number of structs called Process. Process is made up of a pid, burst, and arrival. I would like to sort the queue by arrival, but I don't have the faintest idea of where to begin. Here is some pseudocode to help illustrate what I'm trying to say:

struct Process{
    int pid;
    int burst;
    int arrival;
};

void function(int numProcesses){
    queue<Process> readyQueue;

    // The following loop is a shortened version of my code
    for(int i=0; i<numProcesses;i++){
        readyQueue.push(aProcess);
    }

    // This is where I need help!
    // sort(readyQueue);
}

I'd be appreciative of anyone who could point me in right direction on how to do this, or if it is even possible. Thanks!

4 Answers 4

3

Mostly you need to define operator< for your class:

struct Process{
    int pid;
    int burst;
    int arrival;

    bool operator<(Process const &other) { return arrival < other.arrival; }
};

Once you've done that, std::sort will work fine:

std::sort(std::begin(readyQueue), std::end(readyQueue));
Sign up to request clarification or add additional context in comments.

5 Comments

This is exactly what I've been looking for! I'm very new to anything other than standard arrays. Could you possibly explain how I would call sort? Please and thank you VERY much.
@Rick_Sch: I've included a sample call to std::sort. Minor point: depending on the age of compiler you're using, you may need to use readyQueue.begin() instead of std::begin(readyQueue) (and likewise for end).
One last thing (I hope): I get this error when I try to do that readyQueue.begin() method: error: ‘class std::queue<Process, std::deque<Process, std::allocator<Process> > >’ has no member named ‘begin’ BUT I also get this error if I try the begin(readyQueue) method: ‘begin’ was not declared in this scope
You cannot traverse a queue. You can push and pop, but no iteration through a queue.
Sorry -- hadn't noticed that you were using an actual queue. Easiest to just use an std::deque instead. You may not care about inserting and deleting at both ends, but do care about its giving random access to the contents.
3

You can sort using the standard library std::sort from the '' header. You can either provide a comparator or define an less operators.

struct Process{
    int pid;
    int burst;
    int arrival;
};

    bool operator<(const Process& a, const Process& b) {
          return a.arrival < b.arrival;
    }

    void function(int numProcesses){
        std::dequeue<Process> readyQueue;

        // The following loop is a shortened version of my code
        for(int i=0; i<numProcesses;i++){
             readyQueue.push_back(aProcess);
         }
        std::sort(readyQueue.begin(), readyQueue.end());       
    }

http://en.cppreference.com/w/cpp/algorithm/sort

1 Comment

He wants process sorted by arrival
0

You should use std::priority_queue instead... Otherwise you'll have to sort the queue every time you push something onto it.

Note that you still need to define operator<

2 Comments

I had thought of doing that, but this queue will only ever be added to once, so a priority queue didn't seem necessary.
see this question for a debate on the merits of these solutions speed wise.
0

You want to implement a calendar queue. Don't use the queue data structure for that but a set:

struct Process{
    int pid;
    int burst;
    int arrival;
    bool operator<(Process const& other) const {
      if (arrival == other.arrival) {
        if (pid == other.pid)
          return this < &other;
        return pid < other.pid;
      }
      return arrival < other.arrival;
    }
};

void func() {
  std::set<Process> myQueue;
}

There is no need to explicitly sort, the set will keep the contents sorted at all times and you can always remove the first by eraseing the begin() iterator.

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.