• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <algorithm>

    std::ranges::all_of

    namespace std::ranges {
      template <input_iterator I,
                sentinel_for<I> S,
                class Proj = identity,
                indirect_unary_predicate<projected<I, Proj>> Pred>
      constexpr bool
        all_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
        all_of(R&& r, Pred pred, Proj proj = {});           // (2) C++20
    }
    

    概要

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

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

    戻り値

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

    計算量

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

    #include <algorithm>
    #include <iostream>
    #include <array>
    
    int main() {
      constexpr std::array v = { 3, 1, 4 };
    
      std::cout << std::boolalpha;
    
      // 全ての要素が 5 より小さいか
      constexpr bool result1 = std::ranges::all_of(v, [](int x) { return x < 5; });
      std::cout << result1 << std::endl;
    
      // 全ての要素が 1 であるか
      constexpr bool result2 = std::ranges::all_of(v, [](int x) { return x == 1; });
      std::cout << result2 << std::endl;
    }
    

    出力

    true
    false
    

    実装例

    struct all_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 all_of_impl all_of;
    

    バージョン

    言語

    • C++20

    処理系

    関連項目

    参照