Home C++ Algorithms Functions find_if_not() C++ std::find_if_not() Function 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.
std::find_if_not(v.begin(), v.end(), pred);
std::find_if_not(s.begin(), s.end(), isSpace); Call What 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 template<class InputIt, class UnaryPred>
InputIt find_if_not(InputIt first, InputIt last, UnaryPred q); Parameters Parameter Description first, last Required. The range to search. q Required. A predicate. The search stops at the first element where it returns false.
Return Value Return Value Description Iterator Points at the first element where q returned false. lastReturned when every element passes the test.
C++ Examples 1. First odd number 2. Everything passes 3. Skip leading spaces Example 1 — First odd number Find the first value that is not even.
#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 The predicate says "this number is even". 2, 4 and 6 pass it, so they are skipped. 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.
#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 The rule is "greater than zero", and all three numbers follow it. No element fails, so there is nothing to return. The function returns v.end(), which tells us the whole range passed. Example 3 — Skip leading spaces Find where the real text starts.
#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;
} Text starts at index 3: hello How It Works The predicate is true for a space, so spaces are skipped. The first character that is not a space is h at index 3. it - text.begin() gives the index, and std::string(it, text.end()) copies the rest.Frequently Asked Questions What does std::find_if_not() do? In C++, std::find_if_not() finds the first element that does not satisfy a condition. Include <algorithm> to use it.
Which header and C++ version do I need for std::find_if_not()? Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
What does std::find_if_not() return? It returns an iterator to the first element for which the predicate is false, or the last iterator if every element passes.
How is find_if_not() different from find_if()? find_if() stops at the first element where the predicate is true. find_if_not() stops at the first element where it is false.
Which C++ version adds find_if_not()? C++11. Compile with -std=c++11 or newer.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion