• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <algorithm>

    std::ranges::pop_heap

    namespace std::ranges {
      template <random_access_iterator I,
                sentinel_for<I> S,
                class Comp = ranges::less,
                class Proj = identity>
        requires sortable<I, Comp, Proj>
      constexpr I
        pop_heap(I first,
                 S last,
                 Comp comp = {},
                 Proj proj = {}); // (1) C++20
    
      template <random_access_range R,
                class Comp = ranges::less,
                class Proj = identity>
        requires sortable<iterator_t<R>, Comp, Proj>
      constexpr borrowed_iterator_t<R>
        pop_heap(R&& r,
                 Comp comp = {},
                 Proj proj = {}); // (2) C++20
    }
    

    概要

    ヒープ化された範囲の先頭と末尾を入れ替え、ヒープ範囲を作り直す

    事前条件

    • [first,last) は空でない heap でなければならない。

    効果

    first にある値を last - 1 と交換し、その後 [first,last - 1) が有効な heap となるように配置する。

    戻り値

    last

    計算量

    最大で 2 * log(last - first) 回比較する

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main()
    {
      std::vector<int> v = {3, 1, 4};
    
      std::ranges::make_heap(v);
    
      // 最後尾要素を削除してヒープ化
      std::ranges::pop_heap(v);
      v.pop_back();
    
      std::ranges::sort_heap(v);
    
      for (int x : v) {
        std::cout << x << std::endl;
      }
    }
    

    出力

    1
    3
    

    バージョン

    言語

    • C++20

    処理系

    参照