C++20 sections · 1024 units
Open in Course

count

Count occurrences

I'll show you count(v.begin(), v.end(), value) to get the number of times a specific value appears in a range. It returns an integer count of matching elements. You might use count_if for custom conditions: count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }) counts how many even numbers exist in the vector.

Time complexity is O(n)O(n) since it checks each element. For maps, use the member function m.count(key) instead, which runs in O(log⁡n)O(\log n) due to the tree structure.