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

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

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

履歴 編集

function template
<algorithm>

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

namespace std::ranges {
  template <input_iterator I,
            sentinel_for<I> S,
            class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
  constexpr bool
    is_partitioned(I first,
                   S last,
                   Pred pred,
                   Proj proj = {}); // (1) C++20

  template <input_range R,
            class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
  constexpr bool
    is_partitioned(R&& r,
                   Pred pred,
                   Proj proj = {}); // (2) C++20

  template <execution-policy Ep,
            random_access_iterator I,
            sized_sentinel_for<I> S,
            class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
  bool is_partitioned(Ep&& exec,
                      I first,
                      S last,
                      Pred pred,
                      Proj proj = {}); // (3) C++26

  template <execution-policy Ep,
            sized-random-access-range R,
            class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
  bool is_partitioned(Ep&& exec,
                      R&& r,
                      Pred pred,
                      Proj proj = {}); // (4) C++26
}

概要

与えられた範囲が条件によって区分化されているか判定する。

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

戻り値

[first,last) が空、 または [first,last) が pred によって区分化されているなら true 、そうでなければ false を返す。

つまり、pred を満たす全ての要素が、pred を満たさない全ての要素より前に出現するなら true を返す。

計算量

線形時間。最大で last - first 回 pred が適用される。

例

基本的な使い方

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

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

  auto pred = [](int x) { return x % 2 == 0; };

  // 偶数グループと奇数グループに分ける
  std::ranges::partition(v, pred);

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

  // 偶数グループと奇数グループに分かれているか
  if (std::ranges::is_partitioned(v, pred)) {
    std::cout << "partitioned" << std::endl;
  }
  else {
    std::cout << "not partitioned" << std::endl;
  }
}

出力

4
2
3
1
5
partitioned

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

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

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

  auto pred = [](int x) { return x % 2 == 0; };

  // 並列に偶数グループと奇数グループに分かれているか判定する
  std::cout << std::boolalpha;
  std::cout << "is partitioned? "
            << std::ranges::is_partitioned(std::execution::par, v, pred)
            << std::endl;
}

出力

is partitioned? true

バージョン

言語

  • C++20

処理系

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

参照

  • N4861 25 Algorithms library
  • 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.