Microsoft KB Archive/837697

= How to use the STL PRIORITY_QUEUE class with a custom type =

Article ID: 837697

Article Last Modified on 5/17/2007

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 2005 Express Edition

 Microsoft Visual C++ .NET 2003 Standard Edition

 Microsoft Visual C++ .NET 2002 Standard Edition

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

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

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

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

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

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

-

<div class="notice_section">

<div class="notice_section">

<div class="summary_section">

SUMMARY
''This article describes how to define a Standard Template Library (STL) priority_queue template container adaptor class that uses custom (user-defined) data types such as structures and classes. This article also describes how you can order the elements in the priority_queue container class.

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.''

IN THIS TASK

 * INTRODUCTION
 * Requirements
 * Creating a custom data type
 * Specify QUEUE order
 * Create and access priority_queue variables with custom data types
 * Complete code listing
 * REFERENCES

<div class="summary_section">

INTRODUCTION
This article describes how to use the Standard Template Library (STL) priority_queue template container adaptor class with custom (user-defined) data types such as structures and classes. This article also describes how to order the priority_queue class members by overloading the left angle bracket &quot;<&quot; or the right angle bracket &quot;>&quot; comparison operators. This article also describes how to declare the priority_queue container class variables that contain the custom (user-defined) data members and how to access these variables in your program.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Programming with Standard Template Library(STL) data types and container types

back to the top

Creating a custom data type
The priority_queue class is a template container adaptor class that limits access to the top element of some underlying container type. To limit access to the top element of an underlying container type is always the highest priority. You can add new elements to the priority_queue class and you can examine or remove the top element of the priority_queue class.

To use the priority_queue class with the custom (user-defined) data types, you must define a custom data type as shown in the following code: //Define a custom data type. class Student { public: char* chName; int nAge; Student: chName(&quot;&quot;),nAge(0){} Student( char* chNewName, int nNewAge ):chName(chNewName), nAge(nNewAge){} }; Note To define a structure for the same purpose, you can replace class with struct in this code sample.

back to the top

Specify QUEUE order
You can specify the order of the priority_queue class members by overloading the right angle bracket &quot;<&quot; or the left angle bracket &quot;>&quot; comparison operators as shown in the following code sample: //Overload the < operator. bool operator< (const Student& structstudent1, const Student &structstudent2) {   return structstudent1.nAge > structstudent2.nAge; } //Overload the > operator. bool operator> (const Student& structstudent1, const Student &structstudent2) {   return structstudent1.nAge < structstudent2.nAge; } back to the top

Create and access priority_queue variables with custom data types
The prototype of the priority_queue template class is as follows: template < class Type, class Container=vector<Type>, class Compare=less<typename Container::value_type> > class priority_queue Declare a priority_queue variable that specifies the custom data type and the comparison operator as follows: priority_queue<Student, vector<Student>,less<vector<Student>::value_type> > pqStudent1; You can use different methods of the priority_queue class such as push, pop, empty, and other methods as follows: // Add container elements. pqStudent1.push( Student( &quot;Mark&quot;, 38 ) ); pqStudent1.push( Student( &quot;Marc&quot;, 25 ) ); pqStudent1.push( Student( &quot;Bill&quot;, 47 ) ); pqStudent1.push( Student( &quot;Andy&quot;, 13 ) ); pqStudent1.push( Student( &quot;Newt&quot;, 44 ) );

//Display container elements. while ( !pqStudent1.empty ) { cout << pqStudent1.top.chName << endl; pqStudent1.pop; }

Complete code listing
// The debugger cannot handle symbols that are longer than 255 characters. // STL frequently creates symbols that are longer than 255 characters. // When symbols are longer than 255 characters, the warning is disabled.
 * 1) pragma warning(disable:4786)
 * 2) include &quot;stdafx.h&quot;
 * 3) include


 * 1) using <mscorlib.dll>

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

//Define a custom data type. class Student { public: char* chName; int nAge; Student: chName(&quot;&quot;),nAge(0){} Student( char* chNewName, int nNewAge ):chName(chNewName), nAge(nNewAge){} };

//Overload the < operator. bool operator< (const Student& structstudent1, const Student &structstudent2) {   return structstudent1.nAge > structstudent2.nAge; } //Overload the > operator. bool operator> (const Student& structstudent1, const Student &structstudent2) {   return structstudent1.nAge < structstudent2.nAge; }

int _tmain {   //Declare a priority_queue and specify the ORDER as < //The priorities will be assigned in the Ascending Order of Age priority_queue<Student, vector<Student>,less<vector<Student>::value_type> > pqStudent1;

//declare a priority_queue and specify the ORDER as > //The priorities will be assigned in the Descending Order of Age priority_queue<Student, vector<Student>,greater<vector<Student>::value_type> > pqStudent2; // Add container elements. pqStudent1.push( Student( &quot;Mark&quot;, 38 ) ); pqStudent1.push( Student( &quot;Marc&quot;, 25 ) ); pqStudent1.push( Student( &quot;Bill&quot;, 47 ) ); pqStudent1.push( Student( &quot;Andy&quot;, 13 ) ); pqStudent1.push( Student( &quot;Newt&quot;, 44 ) );

//Display container elements. while ( !pqStudent1.empty ) { cout << pqStudent1.top.chName << endl; pqStudent1.pop; }   cout << endl;

// Add container elements. pqStudent2.push( Student( &quot;Mark&quot;, 38 ) ); pqStudent2.push( Student( &quot;Marc&quot;, 25 ) ); pqStudent2.push( Student( &quot;Bill&quot;, 47 ) ); pqStudent2.push( Student( &quot;Andy&quot;, 13 ) ); pqStudent2.push( Student( &quot;Newt&quot;, 44 ) ); //Display container elements. while ( !pqStudent2.empty ) { cout << pqStudent2.top.chName << endl; pqStudent2.pop; }   cout << endl; return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

back to the top

<div class="references_section">