What count() does in C++
In C++, count() usually refers to std::count, a function in the <algorithm> header. It counts how many elements in a range are equal to a value you give it. The range is described by two iterators: one for the first element and one for the position just past the last element.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {1, 2, 2, 3, 2};
auto twos = std::count(nums.begin(), nums.end(), 2);
std::cout << twos << '\n'; // prints 3
}
nums.begin() points to the first element, and nums.end() points just past the last one, so the range covers the whole vector. The return value is the number of matches, as a signed integer.
If you are unsure how to build the vector first, see c vector initialization.
Counting with a condition
std::count_if takes a predicate instead of a value. The predicate is a function or lambda that returns true for each element you want to count:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5, 6};
auto evens = std::count_if(nums.begin(), nums.end(), [](int x) {
return x % 2 == 0;
});
std::cout << evens << '\n'; // prints 3
}
If the lambda syntax is new to you, see c lambda function.
Counting characters in a string
A std::string provides iterators just like a vector does, so std::count works on it directly:
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string word = "banana";
auto as = std::count(word.begin(), word.end(), 'a');
std::cout << as << '\n'; // prints 3
}
For a closer look at walking through each character, see how to iterate through a string in c.
Counting in an array
std::count also works on a C-style array when you pass std::begin and std::end:
#include <algorithm>
#include <iostream>
#include <iterator>
int main() {
int values[] = {4, 5, 5, 7};
auto fives = std::count(std::begin(values), std::end(values), 5);
std::cout << fives << '\n'; // prints 2
}
std::begin and std::end come from <iterator>, which is why the example includes it.
Counting in a map or set
std::map and std::set have their own count() member function. You pass a key instead of a range. The result is 0 or 1, because their keys are unique:
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> ages = {{"Ana", 31}, {"Ben", 25}};
std::cout << ages.count("Ana") << '\n'; // prints 1
std::cout << ages.count("Cara") << '\n'; // prints 0
}
std::vector and std::string have no count() member, so nums.count(2) will not compile. Use std::count with the range instead.
Common mistakes
- Missing header.
std::countneeds#include <algorithm>. - Confusing count() with size().
size()returns the number of elements, whatever their values.count()counts only the matches. - Signed and unsigned comparison.
std::countreturns a signed value, andsize()returns an unsigned one. Comparing them, as inif (twos == nums.size()), can produce a sign-comparison warning. Convert one side first so both have the same sign. - Types without ==.
std::countcompares each element with==, so a custom type needs anoperator==to be counted.
How fast it is
std::count checks each element once, so the time grows with the length of the range.
Summary
std::count(first, last, value)counts the elements equal tovaluein a range.std::count_ifcounts the elements that pass a condition.- The
count()member of astd::maporstd::settakes a key and returns 0 or 1. std::vectorandstd::stringhave nocount()member, so usestd::countwith their iterators.