我正在尝试测试 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/