C++ wrapper function for Python's "list.count([character])" method? Is this a reasonable C++ wrapper function for Python's "list.count([character])" method?

int count( char CHAR, string STR ) {
    int counter = 0;
    for ( int i = 0 ; i &lt; STR.size() - 1 ; i++  ) {
        if ( STR[i] == CHAR ) {
            counter++;
        }
    }
    return counter;
}
//Edited "for( int i = 0 ; i++ &lt; STR.size-1 ;  )" to "for(int i = 0 ; i &lt; STR.size ; i++)" now it counts the first char of the string =D


Tested it...works exactly like the python &lt;3
When STR is empty, STR.size() returns 0u. As a result, STR.size()-1 becomes maximum value of unsigned size type. i++ &lt; STR.size()-1. This will lead the function to crash.

Also your function will not count CHAR of the first character of STR.

You may want to use standard library.

std::count(begin, end, CHAR)  is just what you are looking for.

for example,

#include &lt;algorithm&gt;
#include &lt;string&gt;

...
char CHAR = 'l';
std::string STR("Hello World!");
ptrdiff_t counter = std::count(STR.begin(), STR.end(), CHAR);
...