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::sort_heap - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. algorithm
  3. sort_heap

最終更新日時: 2026年08月25日 12時11分17秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<algorithm>

std::sort_heap

namespace std {
  template <class RandomAccessIterator>
  void sort_heap(RandomAccessIterator first,
                 RandomAccessIterator last);           // (1) C++98

  template <class RandomAccessIterator>
  constexpr void sort_heap(RandomAccessIterator first,
                           RandomAccessIterator last); // (1) C++20

  template <class RandomAccessIterator, class Compare>
  void sort_heap(RandomAccessIterator first,
                 RandomAccessIterator last,
                 Compare comp);                        // (2) C++98

  template <class RandomAccessIterator, class Compare>
  constexpr void sort_heap(RandomAccessIterator first,
                           RandomAccessIterator last,
                           Compare comp);              // (2) C++20
}

概要

ヒープ化されたイテレータ範囲[first, last)を並べ替える

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

  • RandomAccessIterator は ValueSwappable の要件を満たしていること
  • *first の型は MoveConstructible と MoveAssignable の要件を満たしていること

事前条件

  • [first,last) は有効なヒープであること

効果

ヒープ化されている [first,last) をソートする

戻り値

なし

計算量

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

例

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<int> v = {3, 1, 4};
  std::make_heap(v.begin(), v.end());

  // ヒープ化された範囲をソート
  std::sort_heap(v.begin(), v.end());

  std::for_each(v.begin(), v.end(), [](int x) {
    std::cout << x << std::endl;
  });
}

出力

1
3
4

関連項目

  • ranges::sort_heap

参照

  • P0879R0 Constexpr for swap and swap related functions
  • LWG Issue 2444. Inconsistent complexity for std::sort_heap
    • 計算量の上限が「N log(N)回」から「2N log(N)回」に修正された(sort_heapはN回のpop_heapを行うため)。この修正は欠陥報告(DR)であり、C++17にも遡及して適用される
You are visitor 01402503 — 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.