STL Sample for queue::(push,pop,empty,back,front) Functions

Last reviewed: October 9, 1997
Article ID: Q157622
The information in this article applies to:
  • The Standard C++ Library included with: - Microsoft Visual C++, 32-bit Edition, version 4.2

SUMMARY

The sample code below illustrates how to use the queue::push, queue::pop, queue::empty, queue::back, queue::front, and queue::size STL functions in Visual C++.

MORE INFORMATION

The queue adapter holds objects of the type defined by the type of container supported by the queue. The two containers supported are the list and the deque. Objects are inserted by push() and removed by pop(). front() returns the oldest item in the queue (also known as FIFO), and back() returns the latest item inserted in the queue.

Required Header

   <queue>

Prototypes

   queue::push();
   queue::pop();
   queue::empty();
   queue::back();
   queue::front();
   queue::size();

NOTE: The class/parameter names in the prototypes may not match the version in the header file. Some have been modified to improve readability.

Description

The sample shows queue implementation using list and deque containers.

Sample Code

//////////////////////////////////////////////////////////////////////
//
// Compile options needed: none
//
// <filename> :  queue.cpp
//
// Functions:
//
//    queue::push(), queue::pop(), queue::empty(), queue::back(),
//    queue::front(),queue::size()
//
// Written by Debabrata Sarma
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

/* Compile options needed:none
*/
#include <list>
#include <iostream>
#include <queue>
#include <deque>

// Using queue with list
typedef list<int, allocator<int> > INTLIST; typedef queue<int,INTLIST, allocator<int> > INTQUEUE;

// Using queue with deque
typedef deque<char*, allocator<char*> > CHARDEQUE; typedef queue<char*,CHARDEQUE, allocator<char*> > CHARQUEUE;

void main(void)
{
    int size_q;
    INTQUEUE q;
    CHARQUEUE p;

    // Insert items in the queue(uses list)
    q.push(42);
    q.push(100);
    q.push(49);
    q.push(201);

    // Output the item inserted last using back()
    cout << q.back() << endl;

    // Output the size of queue
    size_q = q.size();
    cout << "size of q is:" << size_q << endl;

    // Output items in queue using front()
    // and use pop() to get to next item until
    // queue is empty
    while (!q.empty())
    {
        cout << q.front() << endl;
        q.pop();

    }

// Insert items in the queue(uses deque)
    p.push("cat");
    p.push("ape");
    p.push("dog");
    p.push("mouse");
    p.push("horse");

    // Output the item inserted last using back()
    cout << p.back() << endl;

    // Output the size of queue
    size_q = p.size();
    cout << "size of p is:" << size_q << endl;

    // Output items in queue using front()
    // and use pop() to get to next item until
    // queue is empty
    while (!p.empty())
    {
        cout << p.front() << endl;
        p.pop();

    }
}

Program Output:

201 size of q is:4 42 100 49 201 horse size of p is:5 cat ape dog mouse horse

REFERENCES

Visual C++ Books On Line: Visual C++ Books:C/C++:Standard C++ Library Reference.


Additional query words: STL STLSample push pop empty back front size
Keywords : STLIss kbcode
Version : WINNT:4.2;
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 9, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.