本文介绍如何定义使用自定义(用户定义的)数据类型的标准模板库(STL) priority_queue
模板容器适配器类。
原始产品版本: Visual C++
原始 KB 数: 837697
总结
本文介绍如何将 STL priority_queue
模板容器适配器类与自定义(用户定义的)数据类型(如结构和类)配合使用。 本文还介绍如何通过重载左尖括号(<)或右尖括号(>)比较运算符对类成员进行排序priority_queue
。 本文还介绍如何声明 priority_queue
包含自定义(用户定义的)数据成员的容器类变量以及如何访问程序中的这些变量。 本文中的信息仅适用于非托管的 Visual C++ 代码。
要求
本文假定你熟悉 STL 数据类型和容器类型的编程。
创建自定义数据类型
该 priority_queue
类是一个模板容器适配器类,用于限制对某些基础容器类型的顶部元素的访问。 限制对基础容器类型顶部元素的访问始终是最高优先级。 可以向 priority_queue
类添加新元素,并且可以检查或删除该类的 priority_queue
顶部元素。
若要将 priority_queue
类与自定义(用户定义的)数据类型一起使用,必须定义自定义数据类型,如以下代码所示:
//Define a custom data type.
class Student
{
public:
char* chName;
int nAge;
Student(): chName(""),nAge(0){}
Student( char* chNewName, int nNewAge ):chName(chNewName), nAge(nNewAge){}
};
注意
若要为同一目的定义结构,可在此代码示例中替换为class
struct
该结构。
指定 QUEUE 顺序
可以通过重载右尖括号或左尖括号比较运算符来指定类成员的顺序 priority_queue
,如以下代码示例所示:
//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;
}
使用自定义数据类型创建和访问priority_queue变量
模板类的 priority_queue
原型如下所示:
template <
class Type,
class Container=vector<Type>,
class Compare=less<typename Container::value_type>
>
class priority_queue
声明一个 priority_queue
变量,该变量指定自定义数据类型和比较运算符,如下所示:
priority_queue<Student, vector<Student>,less<vector<Student>::value_type> > pqStudent1;
可以使用类的不同方法 priority_queue
,例如 push
, pop
, empty
和其他方法,如下所示:
// Add container elements.
pqStudent1.push( Student( "Mark", 38 ));
pqStudent1.push( Student( "Marc", 25 ));
pqStudent1.push( Student( "Bill", 47 ));
pqStudent1.push( Student( "Andy", 13 ));
pqStudent1.push( Student( "Newt", 44 ));
//Display container elements.
while ( !pqStudent1.empty())
{
cout << pqStudent1.top().chName << endl;
pqStudent1.pop();
}
完整代码清单
// 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.
#pragma warning(disable:4786)
#include "stdafx.h"
#include <queue>
#using <mscorlib.dll>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
using namespace System;
//Define a custom data type.
class Student
{
public:
char* chName;
int nAge;
Student(): chName(""),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( "Mark", 38 ));
pqStudent1.push( Student( "Marc", 25 ));
pqStudent1.push( Student( "Bill", 47 ));
pqStudent1.push( Student( "Andy", 13 ));
pqStudent1.push( Student( "Newt", 44 ));
//Display container elements.
while ( !pqStudent1.empty())
{
cout << pqStudent1.top().chName << endl;
pqStudent1.pop();
}
cout << endl;
// Add container elements.
pqStudent2.push( Student( "Mark", 38 ));
pqStudent2.push( Student( "Marc", 25 ));
pqStudent2.push( Student( "Bill", 47 ));
pqStudent2.push( Student( "Andy", 13 ));
pqStudent2.push( Student( "Newt", 44 ));
//Display container elements.
while ( !pqStudent2.empty())
{
cout << pqStudent2.top().chName << endl;
pqStudent2.pop();
}
cout << endl;
return 0;
}
}
必须在 Visual C++ 中添加公共语言运行时支持编译器选项(/clr:oldSyntax),才能成功编译前面的代码示例。 若要在 Visual C++ 中添加公共语言运行时支持编译器选项,请执行以下步骤:
单击“项目”,然后单击“<ProjectName> 属性”。
注意
<ProjectName> 是项目名称的占位符。
展开 配置属性,然后选择“ 常规”。
在右窗格中的公共语言运行时支持项目设置中选择公共语言运行时支持、旧语法(/clr:oldSyntax),选择“应用”,然后选择“确定”。
有关公共语言运行时支持编译器选项的详细信息,请参阅 /clr (公共语言运行时编译)。