从给定字符串中删除特定字符 C++11

Erase a specific character from a given string C++11

我会尝试从以下代码中的给定字符串中删除特定字符。

int main(void){
    string query="a*de*da";
    string org;
    uint8_t rmc='*';

    std::vector<string::const_iterator> wpos;
    for(string::const_iterator itr = org.begin();
        itr!=org.end();
        ++itr){
        if(*itr==rmc){
            wpos.push_back(itr);
        }
    }   

    uint64_t wcnt=0;
    for(auto witr: wpos){
         org.erase( witr-(wcnt++) );
    }   
    query=org;
    return 0;                                                                                                                                                                                                 
} 

在这段代码中,我希望 query="adeda" 但是,我得到了一个错误

 error: no matching function for call to ‘std::basic_string<char>::erase(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >)’
org.erase(witr-wcnt);

我的实验设置是G++ 4.9.2 of devtoolset-3 on CentOS6.7

从C++98到C++11,std::string::erase的签名由

改变
iterator erase(iterator p)

iterator erase(const_iterator p)

g++4.9.2好像还在用老版本。如果将 string::const_iterator 更改为 string::iterator.

,您的示例应该可以编译