Counting Algorithms

Often, you'll need to count the number of elements which meet a particular condition. How many negative numbers are in the vector? How many numbers between one and a hundred? How many short words? All those are counting algorithms. Here's some pseudocode that explains the algorithm:

counter <- 0
examine each item in the collection
    if the item meets the condition then
        count the item

It takes longer to describe this in English than it does to write it in C++. Here's a loop that counts the positive numbers in a vector named v:

int positive{0};
for (auto e : v) 
{ 
   if (e > 0) ++positive; 
}