读书人

find_if中函数对象的使用,该如何处理

发布时间: 2012-03-30 17:32:09 作者: rapoo

find_if中函数对象的使用
[code=C/C++][/code]
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
/*class if_number
{
bool operator()(const char &n)
{
return (n>=48 && n<=57);
}
};*/

bool if_number(const char &n)
{
return (n>=48 && n<=57);
}

int main()
{
string a;
cout<<"ÊäÈ룺";
cin>>a;
string b;
b.insert(b.end(), *(find_if(a.begin(), a.end(), if_number() ) ) );
return 0;
}
[/code]
以上的代码中,我的意思是找出字符串a中的数字,然后传递给b,最后输出,可是不知道哪错了,请高手指点一下!!!谢谢!!!

[解决办法]

C/C++ code
struct if_number { bool operator()(const char &n) { return (n>=48 && n <=57); } };
[解决办法]
C/C++ code
#include  <iostream> #include  <string> #include  <algorithm> using namespace std; class if_number { public:    bool operator()(const char &n)     {         return (n>=48 && n <=57);     } };int main() {     string a;     cout <<"¨º?¨¨?¡êo";     cin>>a;     string b;     b.insert(b.end(), *(find_if(a.begin(), a.end(), if_number())));     return 0; }
[解决办法]
C/C++ code
#include  <iostream> #include  <string> using namespace std; bool if_number(const char &n) { return (n>=48 && n <=57); } int main() {     string a;     cout <<"Input:"<<endl;    cin>>a;     string b;    int i;    for(i = 0; i<a.length(); ++i)    {        if(if_number(a.at(i)))        {            b.insert(b.end(),a.at(i));        }    }    cout<<b<<endl;    return 0; } 

读书人网 >C++

热点推荐