最終更新日時(UTC):
が更新

履歴 編集

function
<queue>

std::priority_queue::コンストラクタ

explicit priority_queue(
             const Compare& x = Compare(),
             const Container& other = Container());       // (1) C++03

explicit priority_queue(const Compare& x = Compare(),
                        Container&& y = Container());     // (1) C++11

priority_queue() : priority_queue(Compare()) {}           // (1) C++20

explicit priority_queue(const Compare& x)
  : priority_queue(x, Container()) {}                     // (2) C++20

priority_queue(const Compare& x, const Container& other); // (3) C++11

priority_queue(const priority_queue&);                    // (4) C++03
priority_queue(const priority_queue&) = default;          // (4) C++11

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last,
               const Compare& x = Compare(),
               const Container& other = Container());     // (5) C++03

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last,
               const Compare& x,
               const Container& other);                   // (6) C++11

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last,
               const Compare& x = Compare(),
               Container&& other = Container());          // (7) C++11

priority_queue(priority_queue&&) = default;               // (8) C++11

template <class Alloc>
explicit priority_queue(const Alloc& alloc);              // (9) C++11

template <class Alloc>
priority_queue(const Compare& x, const Alloc& alloc);     // (10) C++11

template <class Alloc>
priority_queue(const Compare& x,
               const Container& other,
               const Alloc& alloc);                       // (11) C++11

template <class Alloc>
priority_queue(const Compare x&,
               Container&& other,
               const Alloc& alloc);                       // (12) C++11

template <class Alloc>
priority_queue(const priority_queue& que,
               const Alloc& alloc);                       // (13) C++11

template <class Alloc>
priority_queue(priority_queue&& que,
               const Alloc& alloc);                       // (14) C++11

概要

  • (1) : デフォルトコンストラクタ
    • C++03 : 比較関数と元となるコンテナをコピーして構築する。
    • C++11 : 比較関数をコピー、元となるコンテナをムーブして構築する。
    • C++20 : (2)に委譲。
  • (2) : 比較関数のコピーと元となるコンテナをデフォルト構築して構築するコンストラクタ。
  • (3) : 比較関数と、元となるコンテナのコピーから構築するコンストラクタ。
  • (4) : コピーコンストラクタ
  • (5), (6), (7) : イテレータ範囲で優先順位付きキューを構築する。
  • (8) : ムーブコンストラクタ
  • (9) : アロケータを受け取るコンストラクタ
  • (10) : 比較関数とアロケータを受け取るコンストラクタ
  • (11) : 比較関数、元となるコンテナのコピー、アロケータを受け取るコンストラクタ
  • (12) : 比較関数、元となるコンテナの一時オブジェクト、アロケータを受け取るコンストラクタ
  • (13) : アロケータ指定でコピー構築する
  • (14) : アロケータ指定でムーブ構築する

要件

Compare型パラメータxが、狭義の弱順序で定義されていること。

効果

  • (1) :
    • C++03
      1. メンバ変数compxでコピー構築する。
      2. メンバ変数cotherでコピー構築する。
      3. make_heap(c.begin(), c.end(), comp)を呼び出す。
    • C++11
      1. メンバ変数compxでコピー構築する。
      2. メンバ変数cyでムーブ構築する。
      3. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (3) :
    1. メンバ変数compxでコピー構築する。
    2. メンバ変数cotherでコピー構築する。
    3. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (4) :
    1. メンバ変数compxでコピー構築する。
    2. メンバ変数cotherでムーブ構築する。
    3. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (5):
    1. メンバ変数compxでコピー構築する。
    2. メンバ変数cotherでコピー構築する。
    3. c.insert(c.end(), first, last)を呼び出す。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (6) :
    1. c.insert(c.end(), first, last)を呼び出す。
    2. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (7):
    1. メンバ変数compxでコピー構築する。
    2. メンバ変数cotherでムーブ構築する。
    3. c.insert(c.end(), first, last)を呼び出す。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (9) :
    1. メンバ変数cのメモリアロケートにallocを使用する。
    2. メンバ変数comp値初期化する。
  • (10) :
    1. メンバ変数cのメモリアロケートにallocを使用する。
    2. メンバ変数compxで初期化する。
  • (11) :
    1. メンバ変数compxでコピー構築する。
    2. メンバ変数cotherでコピー構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (12) :
    1. メンバ変数compxでコピー構築する。
    2. メンバ変数cotherでムーブ構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (13) :
    1. メンバ変数compque.compでコピー構築する。
    2. メンバ変数cque.cでコピー構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (14) :
    1. メンバ変数compque.compでムーブ構築する。
    2. メンバ変数cque.cでムーブ構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。

#include <iostream>
#include <queue>
#include <vector>
#include <string>

template <class PriorityQueue>
void pop_print(const std::string& name, PriorityQueue& que)
{
  std::cout << name << " : ";
  while (!que.empty()) {
    std::cout << que.top() << ' ';
    que.pop();
  }
  std::cout << std::endl;
}

int main()
{
  // デフォルト構築
  std::priority_queue<int> que1;

  // que1からコピー構築
  std::priority_queue<int> que2 = que1;

  // que2からムーブ構築
  std::priority_queue<int> que3 = std::move(que2);

  // イテレータ範囲から構築
  const std::vector<int> v = {3, 1, 4};
  std::priority_queue<int> que4(v.begin(), v.end());

  // イテレータ範囲、比較関数オブジェクト、コンテナから構築
  const std::vector<int> v2 = {5, 2};
  std::priority_queue<int> que5(v.begin(), v.end(), {}, v2);

  pop_print("que3", que3);
  pop_print("que4", que4);
  pop_print("que5", que5);
}

出力

que3 : 
que4 : 4 3 1 
que5 : 5 4 3 2 1 

処理系

参照