On this page
  1. std::find_if / std::ranges::find_if
  2. Overview
  3. Notes & Pitfalls
  4. Example

std::find_if / std::ranges::find_if

Prev: std::sort | Up: algorithm_and_ranges | Next: std::transform
Rel: Lambdas, std::ranges & std::views
Ref: cppreference — std::ranges::find_if
Tags: #public #prog #cpp #stdlib #algorithms #tier/core


Overview

std::ranges::find_if (<algorithm>) searches linearly for the first element satisfying a predicate and returns an iterator to it (or range.end() if none matches).

Notes & Pitfalls

Example

#include <algorithm>
#include <cassert>
#include <vector>

int main() {
    std::vector<int> nums{1, 3, 8, 5};
    auto it = std::find_if(nums.begin(), nums.end(), [](int n) { return n % 2 == 0; });
    assert(it != nums.end() && *it == 8);
}

Linked from

Related notes