fer1989321 2018-06-08 15:22 采纳率: 100%
浏览 1788
已采纳

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;

}

  • 写回答

4条回答 默认 最新

  • 龙鑫科技 2018-06-10 05:53
    关注

    为了回答你这个问题,我花了一上午的时间, 就为了给你一个结果。

    如下:

    #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;

    }

    结果如下:
    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建