I want to search in a string some substrings using std::search
.
I tried this:
std::string str = "the quick brown fox jumps over the lazy dog";
std::string substr[] = { "fox","dog","bear" };
auto it = std::search(str.begin(), str.end(), substr, substr + 3);
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
And I have these errors :
operator_surrogate_func:no mathing overloaded function found
Failed to specialize function template 'unknown type std::equal_to<void>::operator()
You have an array of substrings so you need to compare each one individually, std::search
can only look for one substring at a time.
std::string str = "the quick brown fox jumps over the lazy dog";
std::string substr[] = { "fox","dog","bear" };
for(auto& sstr : substr){
auto it = std::search(str.begin(), str.end(), sstr.begin(), sstr.end());
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
}
Note that if the substring is not found the return will be the .end()
iterator which points to one past the end of the string.