使用std :: find_if和std :: string [英] using std::find_if with std::string
本文介绍了使用std :: find_if和std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里很蠢,但是我不能得到函数签名的谓词去迭代字符串的find_if:
I'm being stupid here but I can't get the function signature for the predicate going to find_if when iterating over a string:
bool func( char );
std::string str;
std::find_if( str.begin(), str.end(), func ) )
$ b
在这个例子中,google不是我的朋友:(这里有人吗?
In this instance google has not been my friend :( Is anyone here?
推荐答案
#include <iostream>
#include <string>
#include <algorithm>
bool func( char c ) {
return c == 'x';
}
int main() {
std::string str ="abcxyz";;
std::string::iterator it = std::find_if( str.begin(), str.end(), func );
if ( it != str.end() ) {
std::cout << "found\n";
}
else {
std::cout << "not found\n";
}
}
这篇关于使用std :: find_if和std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文