将 STL PRIORITY_QUEUE 类与自定义类型配合使用

本文介绍如何定义标准模板库 (STL) priority_queue 模板容器适配器类,该类使用自定义 (用户定义的) 数据类型。

原始产品版本: Visual C++
原始 KB 编号: 837697

摘要

本文介绍如何将 STL priority_queue 模板容器适配器类与自定义 (用户定义的) 数据类型(如结构和类)配合使用。 本文还介绍了如何通过重载左尖括号 (<) 或右尖括号 (>) 比较运算符来对类成员进行排序priority_queue。 本文还介绍如何声明 priority_queue 包含自定义 (用户定义的) 数据成员的容器类变量,以及如何在程序中访问这些变量。 本文中的信息仅适用于非托管 Visual C++ 代码。

要求

本文假定你熟悉 STL 数据类型和容器类型的编程。

创建自定义数据类型

priority_queue 是模板容器适配器类,用于限制对某些基础容器类型的 top 元素的访问。 限制对基础容器类型的 top 元素的访问始终是最高优先级。 可以向 类添加新元素, priority_queue 并且可以检查或删除类的 priority_queue top 元素。

若要将 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){}
};

注意

若要定义用于相同目的的结构,可以在此代码示例中将 替换为 classstruct

指定队列顺序

可以通过重载右尖括号或左尖括号比较运算符来指定类成员的顺序 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 ,例如 pushpopempty和其他方法,如下所示:

// 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++ 中添加公共语言运行时支持编译器选项,请执行以下步骤:

  1. 单击“ 项目”,然后单击“ <ProjectName> 属性”。

    注意

    <ProjectName> 是项目名称的占位符。

  2. 展开 “配置属性”,然后选择“ 常规”。

  3. 在右侧窗格中的“公共语言运行时支持项目设置”中选择“公共语言运行时支持”、“旧语法 (/clr:oldSyntax) ”,选择“应用”,然后选择“确定”。

有关公共语言运行时支持编译器选项的详细信息,请参阅 /clr (公共语言运行时编译)