名前空間
変種
操作

std::ranges::is_heap_until

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
制約付きアルゴリズム
このメニューのすべての名前は名前空間 `std::ranges` に属します
シーケンスを変更しない操作
シーケンスを変更する操作
パーティション操作
ソート操作
二分探索操作 (ソート済み範囲)
       
       
集合操作 (ソート済み範囲)
ヒープ操作
is_heap_until
     
         
最小/最大操作
       
       
順列操作
畳み込み操作
数値演算
(C++23)            
未初期化ストレージに対する操作
戻り値の型
 
ヘッダー <algorithm> で定義
呼び出しシグネチャ
template< std::random_access_iterator I, std::sentinel_for<I> S,

          class Proj = std::identity,
          std::indirect_strict_weak_order
              < std::projected<I, Proj>> Comp = ranges::less >

constexpr I is_heap_until( I first, S last, Comp comp = {}, Proj proj = {} );
(1) (C++20以降)
template< ranges::random_access_range R, class Proj = std::identity,

          std::indirect_strict_weak_order
              <std::projected
                   <ranges::iterator_t<R>, Proj>> Comp = ranges::less >
constexpr ranges::borrowed_iterator_t<R>

    is_heap_until( R&& r, Comp comp = {}, Proj proj = {} );
(2) (C++20以降)

指定された範囲内で、指定された範囲の先頭から始まる最長の範囲を見つけます。この範囲は、compproj に関して ヒープ を表します。

1) 指定された範囲は [firstlast) です。
2) 指定された範囲は r です。

このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、

目次

[編集] パラメータ

first, last - 調査する要素の範囲
r - 調査する要素の範囲
pred - 射影された要素に適用する述語
proj - 要素に適用する射影

[編集] 戻り値

指定された範囲内の最後のイテレータ iter。これは、以下の条件を満たします。

1) 範囲 [firstiter) は、comp および proj に関してヒープです。
2) 範囲 [ranges::begin(r)iter) は、comp および proj に関してヒープです。

[編集] 計算量

O(N) 回の comp および proj の適用。ここで N

1) ranges::distance(first, last)

[編集] 考えられる実装

struct is_heap_until_fn
{
    template<std::random_access_iterator I, std::sentinel_for<I> S,
             class Proj = std::identity,
             std::indirect_strict_weak_order
                 <std::projected<I, Proj>> Comp = ranges::less>
    constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
    {
        std::iter_difference_t<I> n{ranges::distance(first, last)}, dad{0}, son{1};
        for (; son != n; ++son)
        {
            if (std::invoke(comp, std::invoke(proj, *(first + dad)),
                                  std::invoke(proj, *(first + son))))
                return first + son;
            else if ((son % 2) == 0)
                ++dad;
        }
        return first + n;
    }
 
    template<ranges::random_access_range R, class Proj = std::identity,
             std::indirect_strict_weak_order
                 <std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, Comp comp = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(comp), std::move(proj));
    }
};
 
inline constexpr is_heap_until_fn is_heap_until{};

[編集]

この例では、指定されたベクトルを (バランスの取れた) 二分木としてレンダリングします。

#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <vector>
 
void out(const auto& what, int n = 1)
{
    while (n-- > 0)
        std::cout << what;
}
 
void draw_bin_tree(auto first, auto last)
{
    auto bails = [](int n, int w)
    {
        auto b = [](int w) { out("┌"), out("─", w), out("┴"), out("─", w), out("┐"); };
        n /= 2;
        if (!n)
            return;
        for (out(' ', w); n-- > 0;)
            b(w), out(' ', w + w + 1);
        out('\n');
    };
 
    auto data = [](int n, int w, auto& first, auto last)
    {
        for (out(' ', w); n-- > 0 && first != last; ++first)
            out(*first), out(' ', w + w + 1);
        out('\n');
    };
 
    auto tier = [&](int t, int m, auto& first, auto last)
    {
        const int n{1 << t};
        const int w{(1 << (m - t - 1)) - 1};
        bails(n, w), data(n, w, first, last);
    };
 
    const auto size{std::ranges::distance(first, last)};
    const int m{static_cast<int>(std::ceil(std::log2(1 + size)))};
    for (int i{}; i != m; ++i)
        tier(i, m, first, last);
}
 
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9};
    std::ranges::make_heap(v);
 
    // probably mess up the heap
    v.push_back(2);
    v.push_back(6);
 
    out("v after make_heap and push_back:\n");
    draw_bin_tree(v.begin(), v.end());
 
    out("the max-heap prefix of v:\n");
    const auto heap_end = std::ranges::is_heap_until(v);
    draw_bin_tree(v.begin(), heap_end);
}

出力

v after make_heap and push_back: 
       9               
   ┌───┴───┐       
   5       4       
 ┌─┴─┐   ┌─┴─┐   
 1   1   3   2   
┌┴┐ ┌┴┐ ┌┴┐ ┌┴┐ 
6 
the max-heap prefix of v: 
   9       
 ┌─┴─┐   
 5   4   
┌┴┐ ┌┴┐ 
1 1 3 2

[編集] 関連項目

与えられた範囲が最大ヒープであるかをチェックする
(アルゴリズム関数オブジェクト)[編集]
要素の範囲から最大ヒープを作成する
(アルゴリズム関数オブジェクト)[編集]
最大ヒープに要素を追加する
(アルゴリズム関数オブジェクト)[編集]
最大ヒープから最大の要素を削除する
(アルゴリズム関数オブジェクト)[編集]
最大ヒープを昇順にソートされた要素の範囲に変換する
(アルゴリズム関数オブジェクト)[編集]
最大ヒープである最大のサブ範囲を見つける
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)