カスタム型で STL PRIORITY_QUEUE クラスを使用する

この記事では、カスタム (ユーザー定義) データ型を使用する Standard Template Library (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){}
};

注:

同じ目的で構造体を定義するには、このコード サンプルで を struct に置き換えますclass

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メソッドなど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)]、[適用] の順に選択し、[OK] を選択します

共通言語ランタイム サポート コンパイラ オプションの詳細については、「 /clr (共通言語ランタイム コンパイル)」を参照してください。