C++ std::find_if_not() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++11

Definition

In C++, std::find_if_not() returns an iterator to the first element for which the predicate is false.

It is the mirror of std::find_if(). It helps when you want the first element that breaks a rule, such as the first odd number in a list that should be all even.

Syntax

Add #include <algorithm>, then call std::find_if_not(). Start with these two lines.

C++
std::find_if_not(v.begin(), v.end(), pred);
std::find_if_not(s.begin(), s.end(), isSpace);
CallWhat it does
std::find_if_not(v.begin(), v.end(), pred);Find the first element that fails the test.
std::find_if_not(s.begin(), s.end(), isSpace);Skip leading characters until one fails the test.
Library declarations
C++
template<class InputIt, class UnaryPred>
InputIt find_if_not(InputIt first, InputIt last, UnaryPred q);

Parameters

ParameterDescription
first, lastRequired. The range to search.
qRequired. A predicate. The search stops at the first element where it returns false.

Return Value

Return ValueDescription
IteratorPoints at the first element where q returned false.
lastReturned when every element passes the test.

C++ Examples

Example 1 — First odd number

Find the first value that is not even.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {2, 4, 6, 7, 8};
    auto it = std::find_if_not(v.begin(), v.end(), [](int n) { return n % 2 == 0; });

    if (it != v.end()) {
        std::cout << "First odd value: " << *it << '\n';
    }
    return 0;
}

How It Works

  1. The predicate says "this number is even".
  2. 2, 4 and 6 pass it, so they are skipped.
  3. 7 fails, so find_if_not() stops there and returns an iterator to 7.

Example 2 — Everything passes

See what you get when no element fails the rule.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3};
    auto it = std::find_if_not(v.begin(), v.end(), [](int n) { return n > 0; });

    if (it == v.end()) {
        std::cout << "All numbers are positive\n";
    }
    return 0;
}

How It Works

  1. The rule is "greater than zero", and all three numbers follow it.
  2. No element fails, so there is nothing to return.
  3. The function returns v.end(), which tells us the whole range passed.

Example 3 — Skip leading spaces

Find where the real text starts.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string text = "   hello";
    auto it = std::find_if_not(text.begin(), text.end(), [](char c) { return c == ' '; });

    std::cout << "Text starts at index " << (it - text.begin()) << ": "
              << std::string(it, text.end()) << '\n';
    return 0;
}

How It Works

  1. The predicate is true for a space, so spaces are skipped.
  2. The first character that is not a space is h at index 3.
  3. it - text.begin() gives the index, and std::string(it, text.end()) copies the rest.

Frequently Asked Questions

In C++, std::find_if_not() finds the first element that does not satisfy a condition. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns an iterator to the first element for which the predicate is false, or the last iterator if every element passes.
find_if() stops at the first element where the predicate is true. find_if_not() stops at the first element where it is false.
C++11. Compile with -std=c++11 or newer.

Next: for_each()

Calls a function on every element in a range.

for_each() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

17 people found this page helpful