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

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

最終更新日時: 2026年08月25日 12時11分17秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<algorithm>

std::find_if

namespace std {
  template <class InputIterator, class Predicate>
  InputIterator find_if(InputIterator first,
                        InputIterator last,
                        Predicate pred);               // (1) C++98

  template <class InputIterator, class Predicate>
  constexpr InputIterator find_if(InputIterator first,
                                  InputIterator last,
                                  Predicate pred);     // (1) C++20

  template <class ExecutionPolicy, class ForwardIterator, class Predicate>
  ForwardIterator find_if(ExecutionPolicy&& exec,
                          ForwardIterator first,
                          ForwardIterator last,
                          Predicate pred);             // (2) C++17
}

概要

イテレータ範囲[first, last)の中から、指定された条件を満たす最初の要素を検索する。

戻り値

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

計算量

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

例

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

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

出力

found: 1

実装例

template <class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred) {
  for ( ; first != last; ++first)
    if (pred(*first)) return first;
  return last;
}

関連項目

  • ranges::find_if

参照

  • P0202R3 Add Constexpr Modifiers to Functions in <algorithm> and <utility> Headers
  • P0467R2 Iterator Concerns for Parallel 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.