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

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

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<algorithm>

std::ranges::stable_sort(C++20)

namespace std::ranges {
  template <random_access_iterator I,
            sentinel_for<I> S,
            class Comp = ranges::less,
            class Proj = identity>
    requires sortable<I, Comp, Proj>
  I
    stable_sort(I first,
                S last,
                Comp comp = {},
                Proj proj = {}); // (1) C++20
  template <random_access_iterator I,
            sentinel_for<I> S,
            class Comp = ranges::less,
            class Proj = identity>
    requires sortable<I, Comp, Proj>
  constexpr I
    stable_sort(I first,
                S last,
                Comp comp = {},
                Proj proj = {}); // (1) C++26

  template <random_access_range R,
            class Comp = ranges::less,
            class Proj = identity>
    requires sortable<iterator_t<R>, Comp, Proj>
  borrowed_iterator_t<R>
    stable_sort(R&& r,
                Comp comp = {},
                Proj proj = {}); // (2) C++20
  template <random_access_range R,
            class Comp = ranges::less,
            class Proj = identity>
    requires sortable<iterator_t<R>, Comp, Proj>
  constexpr borrowed_iterator_t<R>
    stable_sort(R&& r,
                Comp comp = {},
                Proj proj = {}); // (2) C++26

  template <execution-policy Ep,
            random_access_iterator I,
            sized_sentinel_for<I> S,
            class Comp = ranges::less,
            class Proj = identity>
    requires sortable<I, Comp, Proj>
  I stable_sort(Ep&& exec,
                I first,
                S last,
                Comp comp = {},
                Proj proj = {}); // (3) C++26

  template <execution-policy Ep,
            sized-random-access-range R,
            class Comp = ranges::less,
            class Proj = identity>
    requires sortable<iterator_t<R>, Comp, Proj>
  borrowed_iterator_t<R>
    stable_sort(Ep&& exec,
                R&& r,
                Comp comp = {},
                Proj proj = {}); // (4) C++26
}

概要

範囲を安定ソートで並べ替える

  • (1): イテレータ範囲を指定する
  • (2): Rangeを直接指定する
  • (3): (1)の並列アルゴリズム版。実行ポリシーを指定する
  • (4): (2)の並列アルゴリズム版。実行ポリシーを指定する

効果

[first,last) の範囲をソートする

戻り値

last

計算量

最大で N log^2(N) (N == last - first)回の比較を行う。ただし、十分なメモリがあれば N log(N) になる。

備考

同じ値が複数あった場合に、ソート前の位置関係が保たれる、「安定ソート」を行う。 一般的にマージソートで実装される。

例

基本的な使い方

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

int main()
{
  std::vector<int> v = {3, 1, 4, 2, 5};

  // 並べ替える
  std::ranges::stable_sort(v);

  for (int i : v) {
    std::cout << i;
  }
  std::cout << std::endl;
}

出力

12345

並列アルゴリズムの例 (C++26)

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

int main()
{
  std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};

  // 並列に安定ソートする
  std::ranges::stable_sort(std::execution::par, v);

  for (int i : v) {
    std::cout << i << ' ';
  }
  std::cout << std::endl;
}

出力

1 1 2 3 3 4 5 5 5 6 9

バージョン

言語

  • C++20

処理系

  • Clang: ??
  • GCC: 10.1.0 ✅
  • Visual C++: 2019 Update 10 ✅

参照

  • N4861 25 Algorithms library
  • P2562R1 constexpr Stable Sorting
    • C++26からconstexprに対応した
  • P3179R9 C++ parallel range algorithms
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.