Namespaces
Variants

std:: sort_heap

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
sort_heap
(C++11)
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <algorithm>
template < class RandomIt >
void sort_heap ( RandomIt first, RandomIt last ) ;
(1) (C++20以降 constexpr)
template < class RandomIt, class Compare >
void sort_heap ( RandomIt first, RandomIt last, Compare comp ) ;
(2) (C++20以降 constexpr)

ヒープ ヒープ [ first , last ) をソート済み範囲に変換します。ヒーププロパティは維持されなくなります。

1) ヒープは operator < (C++20まで) std:: less { } (C++20以降) に関して構成され、 operator < (C++20まで) std:: less { } (C++20以降) に関してソートされます。
2) ヒープは comp に関して構成され、 comp に関してソートされます。

以下のいずれかの条件が満たされる場合、動作は未定義です:

  • [ first , last ) はヒープではありません。
(C++11以前)
(C++11以降)

目次

パラメータ

first, last - バイナリヒープを定義するイテレータのペアで、ソートされた範囲を作成する要素の 範囲 を定義する
comp - 比較関数オブジェクト(つまり、 Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より 小さい 場合に true を返す。

比較関数のシグネチャは以下と同等であるべき:

bool cmp ( const Type1 & a, const Type2 & b ) ;

シグネチャが const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、 値カテゴリ に関係なく型(おそらくconstの) Type1 Type2 のすべての値を受け入れられなければならない(したがって、 Type1 & は許可されない 、また Type1 も、 Type1 に対してムーブがコピーと等価でない限り許可されない (C++11以降) )。
Type1 Type2 は、 RandomIt 型のオブジェクトが間接参照され、それら両方に暗黙的に変換可能でなければならない。

型の要件
-
RandomIt LegacyRandomAccessIterator の要件を満たさなければならない。
-
Compare Compare の要件を満たさなければならない。

計算量

与えられた N std:: distance ( first, last ) として:

1) 最大で 2N⋅log(N) 回の比較を使用 operator < (C++20まで) std:: less { } (C++20以降)
2) 最大で 2N⋅log(N) 回の比較関数 comp の適用。

実装例

sort_heap (1)
template<class RandomIt>
void sort_heap(RandomIt first, RandomIt last)
{
    while (first != last)
        std::pop_heap(first, last--);
}
sort_heap (2)
template<class RandomIt, class Compare>
void sort_heap(RandomIt first, RandomIt last, Compare comp)
{
    while (first != last)
        std::pop_heap(first, last--, comp);
}

#include <algorithm>
#include <iostream>
#include <string_view>
#include <vector>
void println(std::string_view fmt, const auto& v)
{
    for (std::cout << fmt; const auto &i : v)
        std::cout << i << ' ';
    std::cout << '\n';
}
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9};
    std::make_heap(v.begin(), v.end());
    println("after make_heap, v: ", v);
    std::sort_heap(v.begin(), v.end());
    println("after sort_heap, v: ", v);
}

出力:

after make_heap, v: 9 4 5 1 1 3
after sort_heap, v: 1 1 3 4 5 9

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2444 C++98 最大 N⋅log(N) 回の比較が許可されていた 2N⋅log(N) に増加

関連項目

(C++11)
指定された範囲が最大ヒープであるかどうかをチェックする
(関数テンプレート)
最大ヒープである最大の部分範囲を見つける
(関数テンプレート)
要素の範囲から最大ヒープを作成する
(関数テンプレート)
最大ヒープから最大要素を削除する
(関数テンプレート)
要素を最大ヒープに追加する
(関数テンプレート)
最大ヒープを昇順にソートされた要素の範囲に変換する
(アルゴリズム関数オブジェクト)