Wednesday 25 July 2012

queue Library

<queue>


A queue is a container in which elements are inserted at the back and removed from the front. This could also be done with a deque or list, so no new capabilities are provided. A queue does not support iterators or indexing. queue<T> q; // Queue of type T
q.size() // Number of items in q
q.empty() // true if q.size() == 0
q.push(x) // Put x in the back
x=q.back() // The item last pushed, may be assigned to
x=q.front() // The next item to pop, may be assigned to
q.pop() // Remove the front item
A priority_queue is more useful. It sorts the items as they are pushed so that the largest is on top and removed first. priority_queue<T> q; // Element type is T
priority_queue<T, vector<T>, f> q; // Use functoid f(x,y) instead of x < y to sort
q.size(), q.empty() // As before
q.push(x) // Insert x
x=q.top() // Largest item in q, cannot be assigned to
q.pop() // Remove top item

No comments:

Post a Comment