Skip to content
  • Searching & Counting Algorithms · C++

Searching & Counting Algorithms

LearnViaNotes @learnvianotes· 2 min read

<algorithm> has ~100 well-tested, optimised functions that work on iterator ranges. Knowing the common ones replaces most hand-written loops.

Find & count

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

int main() {
    std::vector<int> v{4, 8, 15, 16, 23, 42};

    auto it = std::find(v.begin(), v.end(), 15);
    std::cout << "15 at index " << (it - v.begin()) << '\n';
    if (std::find(v.begin(), v.end(), 99) == v.end()) std::cout << "99 not found\n";

    auto odd = std::find_if(v.begin(), v.end(), [](int x) { return x % 2 == 1; });
    std::cout << "first odd " << *odd << '\n';

    std::cout << std::count(v.begin(), v.end(), 42) << ' '
              << std::count_if(v.begin(), v.end(), [](int x) { return x > 10; }) << '\n';

    std::string s = "mississippi";
    std::cout << std::count(s.begin(), s.end(), 's') << '\n';
}
Output
15 at index 2
99 not found
first odd 15
1 4
4

Predicates over a range

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{2, 4, 6, 7};
    auto even = [](int x) { return x % 2 == 0; };
    std::cout << std::all_of(v.begin(), v.end(), even)
              << std::any_of(v.begin(), v.end(), even)
              << std::none_of(v.begin(), v.end(), [](int x) { return x < 0; }) << '\n';

    auto [mn, mx] = std::minmax_element(v.begin(), v.end());
    std::cout << *mn << ' ' << *mx << ' ' << *std::max_element(v.begin(), v.end()) << '\n';
    std::cout << std::max({3, 9, 1}) << ' ' << std::min(5, 2) << ' ' << std::clamp(15, 0, 10) << '\n';

    std::vector<int> w{2, 4, 6, 8};
    std::cout << std::equal(v.begin(), v.begin() + 3, w.begin()) << '\n';     // first 3 equal?
    auto mm = std::mismatch(v.begin(), v.end(), w.begin());
    std::cout << *mm.first << " vs " << *mm.second << '\n';
}
Output
011
2 7 7
9 2 10
1
7 vs 8

Searching for sequences

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> data{1, 2, 3, 1, 2, 3, 4};
    std::vector<int> pat{2, 3, 4};
    auto it = std::search(data.begin(), data.end(), pat.begin(), pat.end());
    std::cout << "pattern at " << (it - data.begin()) << '\n';
    auto adj = std::adjacent_find(data.begin(), data.end(), [](int a, int b) { return b < a; });
    std::cout << "first descent at " << (adj - data.begin()) << '\n';
}
Output
pattern at 4
first descent at 2
AlgorithmReturnsComplexity
find, find_if, find_if_notIterator to first match or lastO(n)
count, count_ifNumber of matchesO(n)
all_of, any_of, none_ofboolO(n), short-circuits
min_element, max_element, minmax_elementIterator(s)O(n)
equal, mismatchbool / pair of iteratorsO(n)
search, find_end, adjacent_findIteratorO(n·m) / O(n)
binary_search, lower_bound, …On sorted rangesO(log n)

Why prefer std algorithms over raw loops?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report