Namespaces
Variants

std:: stable_sort

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
定義先ヘッダ <algorithm>
template < class RandomIt >
void stable_sort ( RandomIt first, RandomIt last ) ;
(1) (C++26以降 constexpr)
template < class ExecutionPolicy, class RandomIt >

void stable_sort ( ExecutionPolicy && policy,

RandomIt first, RandomIt last ) ;
(2) (C++17以降)
template < class RandomIt, class Compare >
void stable_sort ( RandomIt first, RandomIt last, Compare comp ) ;
(3) (C++26以降 constexpr)
template < class ExecutionPolicy, class RandomIt, class Compare >

void stable_sort ( ExecutionPolicy && policy,

RandomIt first, RandomIt last, Compare comp ) ;
(4) (C++17以降)

範囲 [ first , last ) 内の要素を非降順でソートします。同等の要素の順序は保持されることが保証されています。

1) 要素は sorted されており、 operator < (until C++20) std:: less { } (since C++20) に関してソートされています。
3) 要素は comp に関してソートされます。
2,4) (1,3) と同様ですが、 policy に従って実行されます。
これらのオーバーロードは、以下の条件がすべて満たされる場合にのみオーバーロード解決に参加します:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true であること。

(C++20まで)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true であること。

(C++20以降)

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

(C++11以前)
(C++11以降)

目次

パラメータ

first, last - ソートする要素の範囲を定義するイテレータのペア
policy - 使用する実行ポリシー
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 last - first として:

1,2) O(N·log(N)) 回の比較(十分な追加メモリが利用可能な場合)、そうでない場合は O(N·log 2
(N))
回の比較を使用して実行されます。
3,4) O(N·log(N)) 回の比較子 comp の適用(十分な追加メモリが利用可能な場合)、そうでない場合は O(N·log 2
(N))
回の適用。

例外

ExecutionPolicy という名前のテンプレートパラメータを持つオーバーロードは、 以下のようにエラーを報告します:

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、 ExecutionPolicy 標準ポリシー のいずれかであるとき、 std::terminate が呼び出される。それ以外の ExecutionPolicy については、動作は実装定義である。
  • アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされる。

実装例

実装については libstdc++ および libc++ も参照してください。

注記

この関数は、ソート対象のシーケンスと同等のサイズの一時バッファの割り当てを試みます。割り当てに失敗した場合、効率の低いアルゴリズムが選択されます。

機能テスト マクロ 標準 機能
__cpp_lib_constexpr_algorithms 202306L (C++26) constexpr 安定ソート、オーバーロード ( 1 ) , ( 3 )

#include <algorithm>
#include <array>
#include <iostream>
#include <string>
#include <vector>
struct Employee
{
    int age;
    std::string name; // 比較には関与しない
};
bool operator<(const Employee& lhs, const Employee& rhs)
{
    return lhs.age < rhs.age;
}
#if __cpp_lib_constexpr_algorithms >= 202306L
consteval auto get_sorted()
{
    auto v = std::array{3, 1, 4, 1, 5, 9};
    std::stable_sort(v.begin(), v.end());
    return v;
}
static_assert(std::ranges::is_sorted(get_sorted()));
#endif
int main()
{
    std::vector<Employee> v{{108, "Zaphod"}, {32, "Arthur"}, {108, "Ford"}};
    std::stable_sort(v.begin(), v.end());
    for (const Employee& e : v)
        std::cout << e.age << ", " << e.name << '\n';
}

出力:

32, Arthur
108, Zaphod
108, Ford

関連項目

範囲を昇順にソートする
(関数テンプレート)
範囲の最初のN個の要素をソートする
(関数テンプレート)
要素を相対的な順序を保持しながら2つのグループに分割する
(関数テンプレート)
等しい要素間の順序を保持しながら範囲の要素をソートする
(アルゴリズム関数オブジェクト)