Beginner's Guide

# Standard Library Finding AlgorithmsFinding AlgorithmsFind

## Find / Locate One ElementFind One ElementOne Element

### `find → @value|@end`find

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

int main () {std::vector<int> v {9,0,4,1,8,3,8,2,9};
// find '8' in subrange (as shown in image):
auto i = find(begin(v)+2, begin(v)+7, 8);
// i != end-of-range?
if (i != begin(v)+7) { std::cout << "8 found!\n";… }  // true ⇒ found
// find '9' in entire vector:
auto j = find(begin(v), end(v), 9);
if (j != end(v)) {   // true ⇒ found
auto const value = *j;  // int value = 9
auto const index = distance(begin(v), j);  // index = 0
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
// find next occurrence of '9'
j = find(next(j), end(v), 9);
if (j != end(v)) { std::cout << "9 found!\n";… }  // true ⇒ found}
// find '7' in entire vector:       904183829
auto k = find(begin(v), end(v), 7); //         k
if (k != end(v)) { std::cout << "7 found!\n";… }  // false ⇒ NOT found
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {4,1,3,8,5,8,2};
auto i = std::ranges::find(v, 8);
if (i != end(v)) {   // true ⇒ found
auto const value = *i;  // int value = 8
auto const index = distance(begin(v), i);  // index = 3
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

### `find_if → @(f=true)|@end`find_if

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

int main () {std::vector<int> v {9,0,4,1,8,3,7,2,9};
auto const f = [](int x) {   return x >= 6; };
// find in subrange (as shown in image):
auto i = find_if (begin(v)+2, begin(v)+7, f);
// i != end-of-range?
if (i != begin(v)+7) {  // true ⇒ found
auto const value = *i;  // int value = 8
std::cout << "value: " << value << '\n';
std::cout << "-----------------------\n";}
// find in entire vector:
auto j = find_if (begin(v), end(v), f);
if (j != end(v)) {   // true ⇒ found
auto const value = *j;  // int value = 9
auto const index = distance(begin(v), j);  // index = 0
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {4,1,8,3,7};
auto const f = [](int x) {   return x >= 6; };
auto i = std::ranges::find_if (v, f);
if (i != end(v)) {   // true ⇒ found
auto const value = *i;  // int value = 8
auto const index = distance(begin(v), i);  // index = 2
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

### `find_if_not → @(f=false)|@end`find_if_notC++11

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

int main () {std::vector<int> v {9,0,4,1,3,0,5,2,9};
auto const f = [](int x) {   return x >= 2; };
// find in subrange (as shown in image):
auto i = find_if_not(begin(v)+2, begin(v)+7, f);
// i != end-of-range?
if (i != begin(v)+7) {  // true ⇒ found
auto const value = *i;     // int value = 1
std::cout << "value: " << value << '\n';
std::cout << "-----------------------\n";}
// find in entire vector:
auto j = find_if_not(begin(v), end(v), f);
if (j != end(v)) {   // true ⇒ found
auto const value = *j;  // int value = 0
auto const index = distance(begin(v), j);  // index = 1
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {4,1,3,0};
auto const f = [](int x) {   return x >= 2; };
auto i = std::ranges::find_if_not(v, f);
if (i != end(v)) {   // true ⇒ found
auto const value = *i;  // int value = 1
auto const index = distance(begin(v), i);  // index = 1
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

### `find_last` / `_if`  / `_if_not → view``find_last`/`_if` /`_if_not``find_last`

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

int main () {std::vector<int> v {2,1,7,1,1,5,8};
// NOTE: might not be available yet
// in many standard library implementations!
auto const result = std::ranges::find_last(v, 1);
if (not result.empty()) {   // if found
auto const value = result.front();  // int value = 1
auto const index = distance(begin(v),begin(result));  // index = 4
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
for (int x : result) { std::cout << x << ' '; }  // 1 5 8
std::cout << '\n';
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {4,1,3,8,1};
auto const f = [](int x) {   return x >= 2; };
// NOTE: might not be available yet
// in many standard library implementations!
auto const result = std::ranges::find_last_if (v, f);
if (not result.empty()) {   // if found
auto const value = result.front();  // int value = 8
auto const index = distance(begin(v),begin(result));  // index = 3
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
for (int x : result) { std::cout << x << ' '; }  // 8 1
std::cout << '\n';
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {4,0,3,1,5};
auto const f = [](int x) {   return x >= 2; };
// NOTE: might not be available yet
// in many standard library implementations!
auto const result = std::ranges::find_last_if_not(v, f);
if (not result.empty()) {   // if found
auto const value = result.front();  // int value = 1
auto const index = distance(begin(v),begin(result));  // index = 3
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
for (int x : result) { std::cout << x << ' '; }  // 1 5
std::cout << '\n';
}``````

### `find_first_of → @elem|@end`find_first_of

returns an iterator to the 1st element in range `s` that compares equal to any of the elements in range `w`; otherwise returns `@send` if no such element was found

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

int main () {std::vector<int> s {0,1,3,2,5,7,4,8,9,9};
std::vector<int> w {1,4,6,5,8,7};
// find in subrange of v (as shown in image):
auto i = find_first_of(begin(s)+1, begin(s)+9, begin(w)+1, begin(w)+4);
// i != end-of-range?
if (i != begin(s)+9) {  // true ⇒ found one
auto const value = *i;     // int value = 5
auto const index = distance(begin(s), i);  // index = 4
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
std::cout << "-----------------------\n";}
// find any of 'w' in all of 's':
auto j = find_first_of(begin(s), end(s), begin(w), end(w));
if (j != end(s)) {   // true ⇒ found one
auto const value = *j;  // int value = 1
auto const index = distance(begin(s), j);  // index = 1
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

returns an iterator to the 1st element in range `s` that compares equal to any of the elements in range w; otherwise returns `@end(s)` if no such element was found

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

int main () {std::vector<int> s {3,2,5,7,4,8};
std::vector<int> w {4,6,5};
auto i = std::ranges::find_first_of(s, w);
if (i != end(s)) {   // true ⇒ found one
auto const value = *i;  // int value = 5
auto const index = distance(begin(s), i);  // index = 2
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

## Find Subrange in RangeFind SubrangeSubrange

### `search → @pos|@end`search

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

int main () {std::vector<int> s {0,4,6,5,1,4,6,5,8,9};
std::vector<int> w {1,4,6,5,8,9};
// find subrange of 'w' in subrange of 's' (as shown in image):
auto i = search(begin(s)+1, begin(s)+9, begin(w)+1, begin(w)+4);
// i != end-of-range?
if (i != begin(s)+9) {  // true ⇒ found
auto const value = *i;     // int value = 4
auto const index = distance(begin(s), i);  // index = 1
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
std::cout << "-----------------------\n";}
// find all of 'w' in all of 's':
auto j = search(begin(s), end(s), begin(w), end(w));
if (j != end(s)) {   // true ⇒ found
auto const value = *j;  // int value = 1
auto const index = distance(begin(s), j);  // index = 4
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> s {1,4,6,5,8,4,6,5};
std::vector<int> w {4,6,5};
auto r = std::ranges::search(s, w);
if (not empty(r)) {
for (int x : r) { std::cout << x << ' '; }  // 4 6 5
}
std::cout << '\n';
}``````

### `find_end → @pos|@end`find_end

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

int main () {std::vector<int> s {0,4,6,5,1,4,6,5,8,9};
std::vector<int> w {1,4,6,5,8,9};
// find subrange of 'w' in subrange of 's' (as shown in image):
auto i = find_end(begin(s)+1, begin(s)+9, begin(w)+1, begin(w)+4);
// i != end-of-range?
if (i != begin(s)+9) {  // true ⇒ found
auto const value = *i;     // int value = 4
auto const index = distance(begin(s), i);  // index = 5
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
std::cout << "-----------------------\n";}
// find all of 'w' in all of 's':
auto j = find_end(begin(s), end(s), begin(w), end(w));
if (j != end(s)) {   // true ⇒ found
auto const value = *j;  // int value = 1
auto const index = distance(begin(s), j);  // index = 4
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> s {1,4,6,5,8,4,6,5};
std::vector<int> w {4,6,5};
auto r = std::ranges::find_end(s, w);
if (not empty(r)) {
for (int x : r) { std::cout << x << ' '; }  // 4 6 5
}
std::cout << '\n';
}``````

### `starts_with → true|false`starts_with

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

int main () {std::vector<int> s {0,4,8,6,2,1,9};
std::vector<int> w {9,4,8,6,7,3};
// NOTE: might not be available yet
// in many standard library implementations!
if (std::ranges::starts_with(begin(s)+1, begin(s)+6, begin(w)+1, begin(w)+4) ) {
std::cout << "yes!\n";
}
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> s {4,6,5,8,7,3};
std::vector<int> w {4,6,5};
// NOTE: might not be available yet
// in many standard library implementations!
if (std::ranges::starts_with(s, w) ) {
std::cout << "yes!\n";
}
}``````

### `ends_with → true|false`ends_with

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

int main () {std::vector<int> s {0,4,8,6,2,1,9};
std::vector<int> w {9,6,2,1,7,3};
// NOTE: might not be available yet
// in many standard library implementations!
if (std::ranges::ends_with(begin(s)+1, begin(s)+6, begin(w)+1, begin(w)+4) ) {
std::cout << "yes!\n";
}
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> s {4,6,5,8,7,3};
std::vector<int> w {8,7,3};
// NOTE: might not be available yet
// in many standard library implementations!
if (std::ranges::ends_with(s, w) ) {
std::cout << "yes!\n";
}
}``````

## Find Run of Equal ElementsFind RunRuns Of Elements

### `adjacent_find → @pos|@end`adjacent_find

a custom function(object) for comparing elements can be passed as 3rd argument

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

int main () {std::vector<int> v {5,5,2,8,2,2,3,3,2,8};
// find in subrange (as shown in image):
// i != end-of-range?
if (i != begin(v)+8) {  // true ⇒ found
auto const value = *i;     // int value = 2
auto const index = distance(begin(v), i);  // index = 4
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
std::cout << "-----------------------\n";}
// find in entire vector:
if (j != end(v)) {   // true ⇒ found
auto const value = *j;  // int value = 5
auto const index = distance(begin(v), j);  // index = 0
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

a custom function(object) for comparing elements can be passed as 2nd argument

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

int main () {std::vector<int> v {5,2,8,2,2,3,3};
if (i != end(v)) {   // true ⇒ found
auto const value = *i;  // int value = 2
auto const index = distance(begin(v), i);  // index = 3
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

### `search_n → @pos|@end`search_n

a custom function(object) for comparing elements can be passed as 5th argument

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

int main () {std::vector<int> v {0,5,2,2,8,2,2,2,9,9,9};
// find run of 3 2s in subrange (as shown in image):
const auto n = 3;
auto i = search_n(begin(v)+1, begin(v)+9, n, 2);
// i != end-of-range?
if (i != begin(v)+9) {  // true ⇒ found
auto const value = *i;     // int value = 2
auto const index = distance(begin(v), i);  // index = 5
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';
std::cout << "-----------------------\n";}
// find run of 3 9s in entire vector:
auto j = search_n(begin(v), end(v), n, 9);
if (j != end(v)) {   // true ⇒ found
auto const value = *j;  // int value = 9
auto const index = distance(begin(v), j);  // index = 8
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````

a custom function(object) for comparing elements can be passed as 4th argument

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

int main () {std::vector<int> v {2,2,8,2,2,2,9};
const auto n = 3;
auto r = std::ranges::search_n(v, n, 2);
if (not empty(r)) {   // true ⇒ found
auto const value = r[0];  // int value = 2
auto const index = distance(begin(v), begin(r));  // index = 3
std::cout << "value: " << value << '\n';
std::cout << "index: " << index << '\n';}
}``````