Iterating through a list made up of a custom Class. How do I do it? C++
I am working on an assignment for my Operating Systems class. I am to simulate how a schedular works with Processes. I have a Process class which holds all the information about the processes. I also have a class called scheduler which holds two Process Lists, interactive and Real-Time.
Using a test text file, I am able to read through the file and place Processes into two lists. One for Interactive Processes and one for Real-time processes.
My issue is this. My professor did not let us know if he will put the processes in order of FCFS, as he said that they must be executed in that order. So, what I must now do is iterate through the lists and sort the Processes based on their arrival times. How do I iterate through the list?
I've tried using
list<Process>::iterator it;
for (it=super.interactive.begin() ; it !=super.interactive.end(); it++)
Where super
is the name of the Scheduler object i'm using and interactive
is the Interactive Process List.
But the issue with this is that since it's a list made out of Processes, I can't access the int starttime
that tells me when the processes start because I don't know how to access individual Processes in these lists.
Any help would be much appreciated or suggestions on any other container I could use for thi开发者_如何学JAVAs task would be greatly appreciated.
I first had it set up to use Queues but when it came time to iterate through it, I was told I couldn't. Which is why I've switched to links but i'm not too familiar with those.
My only other idea is to use just dynamic arrays but it would be nice to be able to use Lists because of the push_back()
functions. I wouldn't have to worry about increasing the arrays capacity since with a list and a queue you can just add to the back.
One quality of iterators is that they act like pointers to the data you are iterating over, so if you want starttime
(and it is public), you can do it->starttime
inside of your loop.
But first, you probably don't want std::list. Use std::vector instead, which behaves like a "dynamic array" but handles all of the memory allocation internally. Random access is going to be helpful for keeping a sorted list.
Next, you need a way to sort. Luckily, the standard library has std::sort. You will need to either overload operator<
or provide a BinaryPredicate (as described in the link).
But the issue with this is that since it's a list made out of Processes, I can't access the int starttime that tells me when the processes start because I don't know how to access individual Processes in these lists.
For that, you can do : (*it).something()
, or the better looking : it->something()
.
The only reason I asked is because I needed to sort a bunch of my Process classes. Turns out, the rest of my class is assuming the professor will format the input text as first come first serve so I needn't worry about it. Thanks for the help you two. It did help figuring out other bits of the assignment :D
精彩评论