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 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块