Namespaces
Variants

std::ranges:: stable_partition

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
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
ヘッダーで定義 <algorithm>
呼び出しシグネチャ
template < std:: bidirectional_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
requires std:: permutable < I >
ranges:: subrange < I >

stable_partition ( I first, S last, Pred pred, Proj proj = { } ) ;
(1) (C++20以降)
(C++26以降 constexpr)
template < ranges:: bidirectional_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
requires std:: permutable < ranges:: iterator_t < R >>
ranges:: borrowed_subrange_t < R >

stable_partition ( R && r, Pred pred, Proj proj = { } ) ;
(2) (C++20以降)
(C++26以降 constexpr)
1) 範囲 [ first , last ) 内の要素を、述語 pred true を返す要素の射影 proj が、述語 pred false を返す要素の射影 proj の前に来るように並べ替える。このアルゴリズムは 安定 であり、すなわち要素の相対的な順序は 保持される
2) (1) と同じですが、 r を範囲として使用し、あたかも ranges:: begin ( r ) first として、 ranges:: end ( r ) last として使用するかのように動作します。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

パラメータ

first, last - 要素を並べ替える 範囲 を定義するイテレータ-番兵ペア
r - 並べ替える要素の範囲
pred - 投影された要素に適用する述語
proj - 要素に適用する投影

戻り値

1) 2番目のグループの最初の要素を指すイテレータである pivot last に等しいオブジェクト。
2) (1) と同様、 r が左辺値または borrowed_range 型の場合。それ以外の場合は std::ranges::dangling を返す。

計算量

N = ranges:: distance ( first, last ) が与えられた場合、最悪の計算量は N·log(N) 回のスワップ操作であり、追加のメモリバッファを使用する場合は 𝓞(N) 回のスワップ操作のみです。述語 pred と射影 proj は正確に N 回適用されます。

注記

この関数は一時バッファの割り当てを試みます。割り当てに失敗した場合、効率の低いアルゴリズムが選択されます。

機能テスト マクロ 標準 機能
__cpp_lib_constexpr_algorithms 202306L (C++26) constexpr 安定ソート

実装例

この実装は追加のメモリバッファを使用しないため、効率が低くなる可能性があります。また、 MSVC STL および libstdc++ の実装も参照してください。

struct stable_partition_fn
{
    template<std::bidirectional_iterator I, std::sentinel_for<I> S,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    requires std::permutable<I>
    constexpr ranges::subrange<I>
        operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        first = ranges::find_if_not(first, last, pred, proj);
        I mid = first;
        while (mid != last)
        {
            mid = ranges::find_if(mid, last, pred, proj);
            if (mid == last)
                break;
            I last2 = ranges::find_if_not(mid, last, pred, proj);
            ranges::rotate(first, mid, last2);
            first = ranges::next(first, ranges::distance(mid, last2));
            mid = last2;
        }
        return {std::move(first), std::move(mid)};
    }
    template<ranges::bidirectional_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>> Pred>
    requires std::permutable<ranges::iterator_t<R>>
    constexpr ranges::borrowed_subrange_t<R>
        operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(pred), std::move(proj));
    }
};
inline constexpr stable_partition_fn stable_partition {};

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
namespace rng = std::ranges;
template<std::permutable I, std::sentinel_for<I> S>
constexpr void stable_sort(I first, S last)
{
    if (first == last)
        return;
    auto pivot = *rng::next(first, rng::distance(first, last) / 2, last);
    auto left = [pivot](const auto& em) { return em < pivot; };
    auto tail1 = rng::stable_partition(first, last, left);
    auto right = [pivot](const auto& em) { return !(pivot < em); };
    auto tail2 = rng::stable_partition(tail1, right);
    stable_sort(first, tail1.begin());
    stable_sort(tail2.begin(), tail2.end());
}
void print(const auto rem, auto first, auto last, bool end = true)
{
    std::cout << rem;
    for (; first != last; ++first)
        std::cout << *first << ' ';
    std::cout << (end ? "\n" : "");
}
int main()
{
    const auto original = {9, 6, 5, 2, 3, 1, 7, 8};
    std::vector<int> vi {};
    auto even = [](int x) { return 0 == (x % 2); };
    print("Original vector:\t", original.begin(), original.end(), "\n");
    vi = original;
    const auto ret1 = rng::stable_partition(vi, even);
    print("Stable partitioned:\t", vi.begin(), ret1.begin(), 0);
    print("│ ", ret1.begin(), ret1.end());
    vi = original;
    const auto ret2 = rng::partition(vi, even);
    print("Partitioned:\t\t", vi.begin(), ret2.begin(), 0);
    print("│ ", ret2.begin(), ret2.end());
    vi = {16, 30, 44, 30, 15, 24, 10, 18, 12, 35};
    print("Unsorted vector: ", vi.begin(), vi.end());
    stable_sort(rng::begin(vi), rng::end(vi));
    print("Sorted vector:   ", vi.begin(), vi.end());
}

出力例:

Original vector:        9 6 5 2 3 1 7 8
Stable partitioned:     6 2 8 │ 9 5 3 1 7
Partitioned:            8 6 2 │ 5 3 1 7 9
Unsorted vector: 16 30 44 30 15 24 10 18 12 35
Sorted vector:   10 12 15 16 18 24 30 30 35 44

関連項目

要素の範囲を2つのグループに分割する
(アルゴリズム関数オブジェクト)
要素を2つのグループに分割して範囲をコピーする
(アルゴリズム関数オブジェクト)
範囲が指定された述語で分割されているかどうかを判定する
(アルゴリズム関数オブジェクト)
要素の相対的な順序を保持しながら2つのグループに分割する
(関数テンプレート)