Beginner's Guide
    First Steps
    Input & Output
    Custom Types – Part 1
    Diagnostics
    Standard Library – Part 1
    Function Objects
    Standard Library – Part 2
    Code Organization
    Custom Types – Part 2
    Generic Programming
    Memory Management
    Software Design Basics

    Standard Library Finding Algorithms Finding Algorithms Find

    Find / Locate One Element Find One Element One Element

    find → @value|@end find

    #include <vector>
    #include <algorithm>
    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) { } // 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 }
    // find '7' in entire vector:
    • 9
    • 0
    • 4
    • 1
    • 8
    • 3
    • 8
    • 2
    • 9
    auto k = find(begin(v), end(v), 7);
    • k
    if (k != end(v)) { } // false ⇒ NOT found
    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
    }
    

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

    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
    }
    // 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::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
    }
    

    find_if_not → @(f=false)|@end find_if_not C++11

    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
    }
    // 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::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
    }
    

    find_last / _if  / _if_not → view find_last/_if /_if_not find_last

    C++23
    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
    }
    for (int x : result) { cout << x << ' '; }  // 1 5 8
    
    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
    }
    for (int x : result) { cout << x << ' '; }  // 8 1
    
    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
    }
    for (int x : result) { cout << x << ' '; }  // 1 5
    

    find_first_of → @elem|@end find_first_of

    standard library algorithm 'find_first_of' visual example

    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

    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
    }
    // 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
    }
    
    standard library algorithm 'find_first_of' visual example

    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

    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
    }
    

    Find Subrange in Range Find Subrange Subrange

    search → @pos|@end search

    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
    }
    // 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::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) { cout << x << ' '; }  // 4 6 5
    }
    

    find_end → @pos|@end find_end

    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
    }
    // 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::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) { cout << x << ' '; }  // 4 6 5
    }
    

    starts_with true|false starts_with

    C++20
    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)+1begin(s)+6, begin(w)+1begin(w)+4) ) {
      cout << "yes!\n";
    }
    
    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(sw) ) {
      cout << "yes!\n";
    }
    

    ends_with true|false ends_with

    C++20
    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)+1begin(s)+6, begin(w)+1begin(w)+4) ) {
      cout << "yes!\n";
    }
    
    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(sw) ) {
      cout << "yes!\n";
    }
    

    Find Run of Equal Elements Find Run Runs Of Elements

    adjacent_find → @pos|@end adjacent_find

    standard library algorithm 'adjacent_find' visual example

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

    std::vector<int> v {5,5,2,8,2,2,3,3,2,8};
    // find in subrange (as shown in image):
    auto i = adjacent_find(begin(v)+1, begin(v)+8);
    // 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
    }
    // find in entire vector:
    auto j = adjacent_find(begin(v), end(v));
    if (j != end(v)) {   // true ⇒ found
      auto const value = *j;  // int value = 5
      auto const index = distance(begin(v), j);  // index = 0
    }
    
    standard library algorithm 'adjacent_find' visual example

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

    std::vector<int> v {5,2,8,2,2,3,3};
    auto i = std::ranges::adjacent_find(v);  
    if (i != end(v)) {   // true ⇒ found
      auto const value = *i;  // int value = 2
      auto const index = distance(begin(v), i);  // index = 3
    }
    

    search_n → @pos|@end search_n

    standard library algorithm 'search_n' visual example

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

    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
    }
    // 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
    }
    
    standard library algorithm 'search_n' visual example

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

    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
    }