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

履歴 編集

function template
<algorithm>

std::ranges::none_of(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
    none_of(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
    none_of(R&& r,
            Pred pred,
            Proj proj = {}); // (2) C++20
}

概要

範囲の全ての要素が条件を満たさないかを判定する。

テンプレートパラメータ制約

戻り値

[first,last) あるいは r が空であったり、範囲内の全てのイテレータ i について invoke(pred,invoke(proj, *i))false である場合は true を返し、そうでない場合は false を返す。

計算量

最大で last - firstpred を実行する。

備考

この関数は

all_of(first, last, not_fn(pred));

とほぼ同じであるが、全ての要素が条件を満たしていないということを明示したい場合は none_of() を使う方が意図が伝わりやすい。

#include <algorithm>
#include <iostream>
#include <array>

int main() {
  constexpr std::array v = { 3, 1, 4 };

  std::cout << std::boolalpha;

  // 全ての要素が 3 以上であるか
  constexpr bool result1 = std::ranges::none_of(v, [](int x) { return x < 3; });
  std::cout << result1 << std::endl;

  // 全ての要素が 0 以外であるか
  constexpr bool result2 = std::ranges::none_of(v, [](int x) { return x == 0; });
  std::cout << result2 << std::endl;
}

出力

false
true

実装例

struct none_of_impl {
  template<input_iterator I, sentinel_for<I> S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred>
  constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const {
    for ( ; first != last; ++first)
      if (invoke(pred, invoke(proj, *first))) return false;
    return true;
  }

  template<input_range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
  constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const {
    return (*this)(begin(r), end(r), ref(pred), ref(proj));
  }
};

inline constexpr none_of_impl none_of;

バージョン

言語

  • C++20

処理系

関連項目

参照