Counting the number of occurrences of a given character in a std::string can be done using one function from the STL library: the std::count(...) function. This function takes three parameters: two iterators (the beginning and end of the desired search), and the item you wish to count. This function can be used for any STL container which uses iterators, such as vector and set.

For example, this full program:

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string myString("The bubble sort algorithm is a good algorithm "
                         "for students to learn, but is usually too slow "
                         "for real world applications");
    std::cout << "The string '" << myString << "' has "
              << std::count(myString.begin(), myString.end(), 'a')
              << " occurrences of the letter 'a'" << std::endl;

    return 0;
}

Will print the following:

The string 'The bubble sort algorithm is a good algorithm for students to learn, but is usually too slow for real world applications' has 8 occurrences of the letter 'a'