Apache C++ Standard Library Reference Guide

## queue

Library: Containers

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Constructors
- Member Functions
- Nonmember Operators
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

back() empty() | front() pop() | push() queue() | size()

#### Non-Members

operator!=() | operator>() | operator<() | operator==()

### Summary

A container adaptor that behaves like a queue (first in, first out)

### Synopsis

    #include <queue>

    namespace std {
      template <class T, class Container = deque<T> >
      class queue;
    }

### Description

The queue container adaptor lets a container act as a queue. In a queue, items are pushed into the back of the container and removed from the front. The first items pushed into the queue are the first items to be popped off of the queue (first in, first out, or FIFO).

queue can adapt any container that supports the `front()`, `back()`, `push_back()`, and `pop_front()` operations. In particular, deque and list can be used.

### Interface

    namespace std {

      template <class T, class Container = deque<T> >
      class queue {

        public:

          // typedefs
          typedef typename Container::value_type value_type;
          typedef typename Container::size_type size_type;
          typedef Container container_type;

          // Construct/Copy/Destroy
          explicit queue (const Container& = Container());

          // Accessors
          bool empty () const;
         size_type size () const;
         value_type& front ();
         const value_type& front () const;
         value_type& back ();
         const value_type& back () const;
         void push (const value_type&);
         void pop ();
      };

      // Nonmember Operators

      template <class T, class Container>
      bool operator==(const queue<T, Container>&,
                      const queue<T, Container>&);

      template <class T, class Container>
      bool operator!=(const queue<T, Container>&,
                      const queue<T, Container>&);

      template <class T, class Container>
      bool operator<(const queue<T, Container>&,
                     const queue<T, Container>&);

      template <class T, class Container>
      bool operator>(const queue<T, Container>&,
                     const queue<T, Container>&);

      template <class T, class Container>
      bool operator<=(const queue<T, Container>&,
                      const queue<T, Container>&);

      template <class T, class Container>
      bool operator>=(const queue<T, Container>&,
                      const queue<T, Container>&);
    }

### Constructors

    explicit queue(const Container& = Container());

Creates a queue of zero elements.

### Member Functions

    value_type&
    back();

Returns a reference to the item at the back of the queue (the last item pushed into the queue).

    const value_type&
    back() const;

Returns a constant reference to the item at the back of the queue as a `const_value_type`.

    bool
    empty() const;

Returns `true` if the queue is empty, otherwise `false`.

    value_type&
    front();

Returns a reference to the item at the front of the queue.

    const value_type&
    front() const;

Returns a constant reference to the item at the front of the queue as a `const_value_type`.

    void
    pop();

Removes the item at the front of the queue.

    void
    push(const value_type& x);

Pushes `x` onto the back of the queue.

    size_type
    size() const;

Returns the number of elements on the queue.

### Nonmember Operators

    template <class T, class Container>
      bool operator==(const queue<T, Container>& x,
                       const queue<T, Container>& y);

Returns `true` if `x` is the same as `y`.

    template <class T, class Container>
      bool operator!=(const queue<T, Container>& x,
                       const queue<T, Container>& y);

Returns `!(x==y)`.

    template <class T, class Container>
      bool operator<(const queue<T, Container>& x,
                      const queue<T, Container>& y);

Returns `true` if the queue defined by the elements contained in `x` is lexicographically less than the queue defined by the elements contained in `y`.

    template <class T, class Container>
      bool operator>(const queue<T, Container>& x,
                      const queue<T, Container>& y);

Returns `y < x`.

    template <class T, class Container>
      bool operator<(const queue<T, Container>& x,
                      const queue<T, Container>& y);

Returns `!(y < x)`.

    template <class T, class Container>
      bool operator<(const queue<T, Container>& x,
                      const queue<T, Container>& y);

Returns `!(x < y)`.

### Example

    //
    //  queue.cpp
    //

    #include <deque>      // for deque
    #include <iostream>   // for cout, endl
    #include <list>       // for list
    #include <queue>      // for queue
    #include <string>     // for string

    int main ()
    {
        typedef std::queue<int, std::list<int,
                                          std::allocator<int> > >
                IQueue;

        // Make a queue using a deque container.
        IQueue q;

        // Push a couple of values on and retrieve them.
        q.push (1);
        q.push (2);

        std::cout << q.front () << std::endl;
        std::cout << q.back () << std::endl;

        typedef std::queue<std::string, std::deque<std::string,
                           std::allocator<std::string> > > SQueue;

        // Make a queue of strings using a deque container.
        SQueue qs;

        // Push on a few strings then pop them back off.
        for (int i = 0; i < 10; i++) {
            qs.push (std::string (i + 1, 'a'));
            std::cout << qs.front () << std::endl;
        }

        for (int j = 0; j < 10; j++) {
            std::cout << qs.front () << std::endl;
            qs.pop ();
        }

        // Return 0 if both containers are empty,
        // a non-zero value otherwise.
        return !(2 == q.size () && qs.empty ());
    }

    Program Output:

    1
    2
    a
    a
    a
    a
    a
    a
    a
    a
    a
    a
    a
    aa
    aaa
    aaaa
    aaaaa
    aaaaaa
    aaaaaaa
    aaaaaaaa
    aaaaaaaaa
    aaaaaaaaaa

### See Also

allocator, Containers, priority_queue

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 23.2.3.1