C++ for_each传入参数的问题
//以下代码为何提示重复定义了成员方法,希望大神能给予解答
#include
#include
#include
#include
#include
//#include "myClass.h"
using namespace std;
void eraseStr11(const char & c,string & s)
{
if(!isalpha(int(c)))
s.erase(c);
}
int main()
{
string str;
getline(cin, str);
string strTmp(str);
for_each( str.begin(), str.end(), bind2nd(ptr_fun(eraseStr11), str));
cout << strTmp;
system("pause");
return 0;
}
为了回答你这个问题,我花了一上午的时间, 就为了给你一个结果。
如下:
#include
#include
#include
#include
#include
using std::cout; //只导入 cout 对象
using std::endl; //只导入 endl 对象
using std::cin;
using std::bind1st;
using std::bind2nd;
using std::ptr_fun;
using std::string;
using std::vector;
//过滤掉字符串里面的数字
void eraseStr(const char c,string &s)
{
static int index = 0;
unsigned char data = c - 0x30;
if( (data >= 0) && (data <= 9) )
s.erase(index,1);
else
++index;
}
int main()
{
vector<char> v_c;
string str;
cout << "请输入一行字符:" ;
getline(cin, str);
string strTmp(str);
cout << "strTmp = " << strTmp << endl;
for ( int i = 0; i < str.size() ; i++ )
v_c.push_back(str[ i ]);
for_each( v_c.begin(), v_c.end(), bind2nd(ptr_fun(eraseStr),strTmp) );
cout << strTmp;
return 0;
}
c++11已经出来了这么多年,使用lambda表达式会更加方便呀。
string strTemp = str;
for_each( str.begin(), str.end(), &strTemp{if(!isalpha(int(c)))s.erase(c);});