【发布时间】:2021-01-03 04:38:37
【问题描述】:
我想使用std::search在字符串中搜索一些子字符串。
我试过这个:
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;
我有这些错误:
operator_surrogate_func:no mathing overloaded function found
Failed to specialize function template 'unknown type std::equal_to<void>::operator()
【问题讨论】:
-
std::search()在这种情况下所做的是尝试将单个字符与 std::strings 进行比较,这是不可能的,因此会出现错误。 -
std::search搜索 one 字符串。如果你想从一个数组中搜索三个字符串,你需要使用std::search和一个循环。
标签: c++ algorithm search substring stdstring