This question is not hard in terms of getting solution, but I would like to know if any C++ function or algorithm is available to solve it.
I got this thought while going through this question Count character occurrences in a string in C++
So would like to know if we have any option other than writing a function from scratch to check if there is a specific number of occurrences of a character in the string. For example let us say:
std::string s = "a_b_c_d_e_f_g_h_i_j_k_l_m";
and we want to find if there are at least 2 '_' in the string.
If we use std::count
it will return the count of all '_'. std::count_if
will also behave in similar way.
I can write a code to loop through the string and break as soon as the count reaches 2, but I would like to know if we have some existing solution in C++ algorithms or functions.
Thought process here is, if we get a very long string as an input and criteria of doing something is based on whether there is at least n number of occurrences of a specific character, then it is a waste to traverse through the whole string.