c++ - std::search() 的返回类型

标签 c++ search stl

我正在尝试测试 Stroustup 书中的 search() 示例。

string quote("why waste time learning, when ignorance is instantaneous?");

bool in_quote(const string& s){
    char* p = search(quote.begin(), quote.end(), s.begin(), s.end());
    return p != quote.end();
}

void test(){
    bool b1 = in_quote("learning"); // b1=true
    bool b2 = in_quote("lemming"); // b2=false
}

但是我得到以下错误:

error C2440: 'initializing' : cannot convert from
'std::_String_iterator<_Elem,_Traits,_Alloc>' to 'char *'

看起来返回类型不对。我也尝试了 string::iterator,但得到了同样的错误。那么,正确的类型应该是什么,它应该是容器的迭代器类型吗?谢谢

最佳答案

不关心返回类型怎么样? :)

bool in_quote(const string& s){
    return search(quote.begin(), quote.end(), s.begin(), s.end()) != quote.end();
}

关于c++ - std::search() 的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014798/

相关文章:

c++ - 为什么在同一个标​​头中转发声明和定义的理由是什么?

c++ - 访问 map 数据

c++ - std::list::splice, list&& VS list&

java - 如何在文本文件中查找某些单词,然后在Java中查找数字?

php - 为什么使用dom而不是regex来解析网页?

sql - 如何显示相关(建议)关键字?

c++ - 使用与索引不同的类访问 C++ 映射

c++ - make_pair 是原子的吗?

php - 将 c++ 函数调用到 PHP 代码中

c++ - Box2d C++ AccessViolation 使用 b2fixture out 方法