Saturday, May 15, 2010

STL QUEUE

STL QUEUE


  • queues are a type of container adaptors.
  • Operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.
  • queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access it elements. Elements are pushed into the "back" of the specific container and popped from its "front".
  • The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:
  1. front()
  2. back()
  3. push_back()
  4. pop_front()
  • Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.
Example:

// queue::front
# include < iostream >
# include < queue >
using namespace std;

int main ()
{
  queue< int > myqueue;

  myqueue.push(77);
  myqueue.push(16);

  myqueue.front() -= myqueue.back();    // 77-16=61

  cout << "myqueue.front() is now " << myqueue.front() << endl;

  return 0;
}

Output:
myqueue.front() is now 61

No comments:

Post a Comment