使用 std::search 搜索 std::string 中的子字符串
Using std::search to search substrings in a std::string
我想使用 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
的最后两个参数应该是你正在寻找的字符序列的迭代器(更准确地说,你的迭代器和你的迭代器的引用类型必须是可比较的),而你的substr
变量,如果数组 std::string
。所以范围 [substr, substr+3)
实际上并不代表你的“fox”或“dog”或“bear”,而是指向字符串“fox”的指针和指向数组末尾的指针。
尝试以下操作:
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[0].begin(), substr[0].end());
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
it = std::search(str.begin(), str.end(), substr[1].begin(), substr[1].end());
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
it = std::search(str.begin(), str.end(), substr[2].begin(), substr[2].end());
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
return 0;
首先它会在您的字符串中搜索“fox”,然后是“dog”,然后是“bear”。未找到“熊”,生成的迭代器指向 str.end()
.
您有一组子字符串,因此您需要单独比较每个子字符串,std::search
一次只能查找一个子字符串。
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;
}
请注意,如果未找到子字符串,return 将是 .end()
迭代器,它指向字符串末尾后的一个。
我想使用 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
的最后两个参数应该是你正在寻找的字符序列的迭代器(更准确地说,你的迭代器和你的迭代器的引用类型必须是可比较的),而你的substr
变量,如果数组 std::string
。所以范围 [substr, substr+3)
实际上并不代表你的“fox”或“dog”或“bear”,而是指向字符串“fox”的指针和指向数组末尾的指针。
尝试以下操作:
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[0].begin(), substr[0].end());
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
it = std::search(str.begin(), str.end(), substr[1].begin(), substr[1].end());
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
it = std::search(str.begin(), str.end(), substr[2].begin(), substr[2].end());
std::cout << "substring found at position :" << (it - str.begin()) << std::endl;
return 0;
首先它会在您的字符串中搜索“fox”,然后是“dog”,然后是“bear”。未找到“熊”,生成的迭代器指向 str.end()
.
您有一组子字符串,因此您需要单独比较每个子字符串,std::search
一次只能查找一个子字符串。
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;
}
请注意,如果未找到子字符串,return 将是 .end()
迭代器,它指向字符串末尾后的一个。