最終更新日時(UTC):
が更新

履歴 編集

function template
<algorithm>

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

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

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

概要

与えられた範囲から条件によって区分化されている位置を得る。

事前条件

  • [first,last)pred によって区分化されていなければならない。つまり、pred を満たす全ての要素が、pred を満たさない全ての要素より前に出現してなければならない。

戻り値

all_of(first, mid, pred)none_of(mid, last, pred)true であるようなイテレータ mid を返す。

計算量

pred が O(log(last - first)) 回適用される。

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

void print(const char* name, const std::vector<int>& v)
{
  std::cout << name << " : ";
  std::for_each(v.begin(), v.end(), [](int x) {
    std::cout << x << ",";
  });
  std::cout << std::endl;
}

bool is_even(int x) { return x % 2 == 0; }

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

  std::ranges::partition(v, is_even);

  // 偶数グループと奇数グループに分かれた位置を得る
  auto it = std::ranges::partition_point(v, is_even);

  print("v", v);
  std::cout << *it << std::endl;
}

出力

v : 4,2,3,1,5,
3

バージョン

言語

  • C++20

処理系

参照