boost::range::count

References

Headers

boost::range::count is available by including any of the following headers:

  • boost/range/algorithm/count.hpp or
  • boost/range/algorithm.hpp

Examples

count.cpp

#include <functional>
#include <iostream>
#include <boost/range/algorithm.hpp>

bool is_m_or_n(char c) {
    return c == 'm' || c == 'n';
}

int main() {
    std::string s = "abcddefghijkklmmnoopqrstuvwxyyyz";

    int num_o = boost::range::count(s, 'o');
    int num_mn = boost::range::count_if(s, is_m_or_n);

    std::cout << "Input string: \"" << s << "\"" << std::endl;
    std::cout << "Number of 'o' characters: " << num_o << std::endl;
    std::cout << "Number of 'm' and 'n' characters: " << num_mn << std::endl;
    return 0;
}

Output:

Input string: "abcddefghijkklmmnoopqrstuvwxyyyz"
Number of 'o' characters: 2
Number of 'm' and 'n' characters: 3

 

Boost Range for Humans

This reference is part of Boost Range for Humans. Click the link to the overview.