Microsoft KB Archive/157622

= How to use the member functions of the STL queue class in Visual C++ =

Article ID: 157622

Article Last Modified on 8/16/2005

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q157622

<div class="notice_section">

NOTE: Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

<div class="summary_section">

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++.

<div class="moreinformation_section">

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
<pre class="fixed_text">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 // // : 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: /GX using namespace std;
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 1) include

using namespace std; // std c++ libs implemented in std #endif
 * 1) if _MSC_VER > 1020  // if VC++ version is > 4.2

// Using queue with list

typedef list<int, allocator > INTLIST; typedef queue<int,INTLIST > INTQUEUE;

// Using queue with deque

typedef deque<char*, allocator<char*> > CHARDEQUE; typedef queue<char*,CHARDEQUE> 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: <pre class="fixed_text">201 size of q is:4 42 100 49 201 horse size of p is:5 cat ape dog mouse horse

<div class="references_section">