I am trying to write a function that takes two strings as arguments and returns the total count of how many times each character of the second string appears in the first.
For example, i = count("abracadabra", "bax");
would return 7
.
I am looking to utilize the STL. I have written the following function that counts how many occurrences of one char happens in a string, but calling this function on a loop to solve the problem above seems quite inefficient.
int count(const std::string& str, char c)
{
int count = 0;
size_t pos = str.find_first_of(c);
while (pos != std::string::npos)
{
count++;
pos = str.find_first_of(c, pos + 1);
}
return count;
}