GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::priority_queue::コンストラクタ - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. queue
  3. priority_queue
  4. コンストラクタ

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function
<queue>

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

explicit
  priority_queue(const Compare& x = Compare(),
                 const Container& other = Container()); // (1) C++98
explicit
  priority_queue(const Compare& x = Compare(),
                 Container&& y = Container());          // (1) C++11
priority_queue()
  : priority_queue(Compare()) {}                        // (1) C++20
constexpr
  priority_queue()
    : priority_queue(Compare()) {}                      // (1) C++26

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

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

priority_queue(const priority_queue&);                     // (4) C++98
priority_queue(const priority_queue&) = default;           // (4) C++11
constexpr priority_queue(const priority_queue&) = default; // (4) C++26

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

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

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

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

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

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

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

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

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

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

template <class InputIterator, class Alloc>
priority_queue(InputIterator first,
               InputIterator last,
               const Compare& x,
               const Alloc& alloc); // (15) C++23
template <class InputIterator, class Alloc>
constexpr
  priority_queue(InputIterator first,
                 InputIterator last,
                 const Compare& x,
                 const Alloc& alloc); // (15) C++26

template <class InputIterator, class Alloc>
priority_queue(InputIterator first,
               InputIterator last,
               const Compare& x,
               const Container& other,
               const Alloc& alloc); // (16) C++23
template <class InputIterator, class Alloc>
constexpr
  priority_queue(InputIterator first,
                 InputIterator last,
                 const Compare& x,
                 const Container& other,
                 const Alloc& alloc); // (16) C++26

template <class InputIterator, class Alloc>
priority_queue(InputIterator first,
               InputIterator last,
               const Compare& x,
               Container&& other,
               const Alloc& alloc); // (17) C++23
template <class InputIterator, class Alloc>
constexpr
  priority_queue(InputIterator first,
                 InputIterator last,
                 const Compare& x,
                 Container&& other,
                 const Alloc& alloc); // (17) C++26

template <container-compatible-range<T> R>
priority_queue(from_range_t, R&& rg,
               const Compare& x = Compare()); // (18) C++23
template <container-compatible-range<T> R>
constexpr
  priority_queue(from_range_t, R&& rg,
                 const Compare& x = Compare()); // (18) C++26

template <container-compatible-range<T> R, class Alloc>
priority_queue(from_range_t, R&& rg,
               const Compare& x,
               const Alloc& alloc); // (19) C++23
template <container-compatible-range<T> R, class Alloc>
constexpr
  priority_queue(from_range_t, R&& rg,
                 const Compare& x,
                 const Alloc& alloc); // (19) C++26

template <container-compatible-range<T> R, class Alloc>
priority_queue(from_range_t, R&& rg,
               const Alloc& alloc); // (20) C++23
template <container-compatible-range<T> R, class Alloc>
constexpr
  priority_queue(from_range_t, R&& rg,
                 const Alloc& alloc); // (20) C++26

概要

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

要件

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

テンプレートパラメータ制約

  • (5), (6), (7), (15), (16), (17) :
    • C++23 : InputIteratorが入力イテレータの要件を満たすこと。

効果

  • (1) :
    • C++98
      1. メンバ変数compをxでコピー構築する。
      2. メンバ変数cをotherでコピー構築する。
      3. make_heap(c.begin(), c.end(), comp)を呼び出す。
    • C++11
      1. メンバ変数compをxでコピー構築する。
      2. メンバ変数cをyでムーブ構築する。
      3. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (3) :
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cをotherでコピー構築する。
    3. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (5):
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cをotherでコピー構築する。
    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. メンバ変数compをxでコピー構築する。
    2. メンバ変数cをotherでムーブ構築する。
    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. メンバ変数compをxで初期化する。
  • (11) :
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cをotherでコピー構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (12) :
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cをotherでムーブ構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (13) :
    1. メンバ変数compをque.compでコピー構築する。
    2. メンバ変数cをque.cでコピー構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (14) :
    1. メンバ変数compをque.compでムーブ構築する。
    2. メンバ変数cをque.cでムーブ構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (15) :
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cのメモリアロケートにallocを使用する。
    3. c.insert(c.end(), first, last)を呼び出す。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (16) :
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cをotherでコピー構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. c.insert(c.end(), first, last)を呼び出す。
    5. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (17) :
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cをotherでムーブ構築する。
    3. メンバ変数cのメモリアロケートにallocを使用する。
    4. c.insert(c.end(), first, last)を呼び出す。
    5. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (18) :
    1. メンバ変数compをxでコピー構築する。
    2. c.insert(c.end(), ranges::begin(rg), ranges::end(rg))を呼び出す。
    3. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (19) :
    1. メンバ変数compをxでコピー構築する。
    2. メンバ変数cのメモリアロケートにallocを使用する。
    3. c.insert(c.end(), ranges::begin(rg), ranges::end(rg))を呼び出す。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。
  • (20) :
    1. メンバ変数compを値初期化する。
    2. メンバ変数cのメモリアロケートにallocを使用する。
    3. c.insert(c.end(), ranges::begin(rg), ranges::end(rg))を呼び出す。
    4. make_heap(c.begin(), c.end(), comp)を呼び出す。

例

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

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 

処理系

  • Clang: 9.0.1 ✅
  • GCC: 4.7.0(アロケータ付き初期化以外は使用可能) ✅
  • Visual C++: ??

参照

  • P0935R0 Eradicating unnecessarily explicit default constructors from the standard library
  • LWG Issue 1194. Unintended queue constructor
    • C++11で、アロケータを受け取るコンストラクタ群が、uses_allocator<container_type, Alloc>::valueがtrueである場合にのみオーバーロード解決に参加するよう制約された。制約がないと、Allocにコンテナが構築可能な任意の型を渡せてしまい、priority_queue<int> q(5);のような意図しない構築が通ってしまうため
  • LWG Issue 1199. Missing extended copy constructor in container adaptors
    • C++11で、アロケータを指定する拡張コピーコンストラクタが追加された。ムーブ版は既に用意されていたが、コピー版が漏れていた
  • LWG Issue 2537. Constructors for priority_queue taking allocators should call make_heap
    • C++17で、アロケータを受け取るコンストラクタでもmake_heapを呼び出すことが明記された
    • この修正は欠陥報告(DR)であり、C++11以降に遡及して適用される。元の文言を厳密に読むと呼び出し側がヒープ化済みのコンテナを渡す必要があるという意図しない解釈になり、処理系は当初からmake_heapを呼んでいたため
  • P3372R3 constexpr containers and adaptors
  • LWG Issue 3506. Missing allocator-extended constructors for priority_queue
    • C++23で、イテレータ範囲を受け取るアロケータ拡張コンストラクタ(15)〜(17)が追加された
  • LWG Issue 3522. Missing requirement on InputIterator template parameter for priority_queue constructors
    • C++23で、イテレータ範囲を受け取るコンストラクタが、入力イテレータでない型が推論された場合にオーバーロード解決に参加しないという制約が追加された
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.