• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <algorithm>

    std::ranges::find_if_not

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

    概要

    範囲の中から、指定された条件を満たさない最初の要素を検索する。

    戻り値

    [first,last) あるいは r 内のイテレータ i について、invoke(pred,invoke(proj, *i)) == false である最初のイテレータを返す。そのようなイテレータが見つからなかった場合は last を返す。

    計算量

    最大で last - first述語による比較を行う

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    int main() {
      std::vector<int> v = { 3, 1, 4 };
      // 3ではない最初の要素を検索する
      auto result = std::ranges::find_if_not(v, [](int x) { return x == 3; });
      if (result == v.end()) {
        std::cout << "not found" << std::endl;
      } else {
        std::cout << "found: " << *result << std::endl;
      }
    }
    

    出力

    found: 1
    

    実装例

    struct find_if_not_impl {
      template<input_iterator I, sentinel_for<I> S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred>
      constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const {
        for ( ; first != last; ++first)
          if (!invoke(pred, invoke(proj, *first)))
            return first;
      }
    
      template<input_range R, class Proj = identity, indirect_unary_predicate <projected<iterator_t<R>, Proj>> Pred>
      constexpr borrowed_iterator_t<R> operator()(R&& r, Pred pred, Proj proj = {}) const {
        return (*this)(begin(r), end(r), ref(pred), ref(proj));
      }
    };
    
    inline constexpr find_if_not_impl find_if_not;
    

    バージョン

    言語

    • C++20

    処理系

    参照