标准模板库 (STL) priority_queue

本文介绍如何在C++标准模板库(STL)的priority_queue模板容器适配器类中使用自定义数据类型,包括如何重载比较运算符来指定优先级顺序,以及如何声明和操作包含这些自定义数据类型的priority_queue变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://support.microsoft.com/kb/837697/zh-cn

 

本文介绍如何使用标准模板库 (STL) priority_queue 模板容器适配器类具有自定义 (用户) 定义数据类型如结构和类。 本文还介绍如何通过重载左尖括号订购 priority_queue 类成员 " < " 或右侧角度括号 > " 比较运算符。 还介绍如何声明 priority_queue 容器类变量包含自定义 (用户) 定义数据成员以及如何访问这些变量程序中。


回到顶端
要求
本文假定您已熟悉以下主题:
与常用模板 Library(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){}
};
注意 要定义用于同一目的, 结构与此代码示例中 结构 更换 类 。


回到顶端
指定 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 类如 强制 空 , pop 、 的不同方法和其他方法如下:
// 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++ 2005 以成功编译前面的代码示例中 clr:oldSyntax。 要在 VisualC++2005, 添加公共语言运行库支持编译器选项请按照下列步骤:
单击 项目 , 然后单击 < ProjectName > 属性 .

注意 < ProjectName > 是占位符代表的项目名称。
依次展开 ConfigurationProperties , 常规 。
单击以选中 公共语言运行库支持、 旧语法 () CommonLanguageRuntime 支持 项目设置在右窗格中, 中单击 应用 , 然后单击 确定 / clr:oldSyntax。
有关公共语言运行库支持编译器选项, 请访问以下 MicrosoftWeb 站点获取:
/Clr (公共语言运行库编译)
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx (http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx)
回到顶端
参考
请有关使用 priority_queue 类成员, 访问以下 Microsoft Developer Network (MSDN) Web 站点: priori...

请有关使用 priority_queue 类成员, 访问以下 Microsoft Developer Network (MSDN) Web 站点:
priority_queue 函数 (STL 示例)
http://msdn2.microsoft.com/en-us/library/z3k60dkc(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/z3k60dkc(VS.71).aspx)
priority_queue::priority_queue
http://msdn2.microsoft.com/en-us/library/6w541ec5(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/6w541ec5(vs.71).aspx)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值