简体   繁体   中英

Adding Items to a Queue

I am supposed to simulate a one runway airport's operations over a given period of time. What I have done is create queues to hold planes that are ready to take off and land respectively. I created a class to represent a plane due to the fact that I am required to monitor how long it is kept in the queue. So to add a "plane" to either queue, a condition is tested(a function that returns true/false based on randomness) and if true a plane is added. My problem is that is there a way a new "plane" can be declared and then added to the queue? For example, assuming "landing" is an already declared queue:

if(condition)
    create plane
    landing.push(created plane)

If you deem a queue as a special array, it might be easier for you. You push the object onto the queue, like a BigInt or a plane object in your case (depending how you implement the queue). You can either call the constructor of Plane in the parenthesis or create an plane object separately and push it onto the queue. So most intuitively:

landing.push(Plane(a,b...));

or:

Plane p = new Plane(a,b...); landing.push(p);

If you wrote your own queue class, then it really depends on how you implement the push method. Do you simply takes a reference to a plane object and create a node for it or do you create the plane object in the push method?

Yes? Its not very clear what you're asking. You would just run your constructor or what have you to create the plane and push it onto the queue.

From your question, it seems all you need to do is this:

if (condition)
    landing.push(Plane(/*argumnts, if any */));

I'm assuming that your container creates copies.

Example:

#include <queue>

class Plane
{
   public:
     Plane() {}
};

std::queue<Plane> landing;

int main()
{
   landing.push(Plane());
}

Live Example: http://ideone.com/TyTtlc

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM