C++ STL 字符串擦除



我做了一个小程序,可以从句子中删除单词。每次我尝试运行该程序时,它都会给我这些错误

错误

2 错误 C2040:"==":"int"在间接级别上有所不同 来自 'const char [4]' c:\Program Files (x86)\Microsoft Visual Studio 12.0\vc\include\x实用程序行:3026 列:1 STL 字符串擦除

错误

1 错误 C2446:"==":没有从"常量字符 *"到 'int' c:\program files (x86)\Microsoft Visual Studio 12.0\vc\include\x实用程序行:3026 列:1 STL 字符串擦除

这也是我的代码

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    string sample("hello world");
    cout << "The sample string is: ";
    cout << sample << endl;
    //erasing world
    cout << "Erasing world" << endl;
    sample.erase(5, 10);
    cout << sample << endl;
    //finding h and erasing it
    string::iterator iCharH = std::string::find(sample.begin(), sample.end(), "h");
    cout << "finding h and erasing it" << endl;
    if (iCharH != sample.end()){
        sample.erase(iCharH);
    }
    cout << sample << endl;
    //erasing entirely
    sample.erase(sample.begin(), sample.end());
    if (sample.length() == 0){
        cout << "The string is empty" << endl;
    }
    system("pause");
    return 0;
}

你需要的 - 正如克里斯在评论中所写的 - 是std::string::find。原因基本上是您希望删除单词,而不是单个字符。

我认为对

std::string::erasestd::string::find成员函数存在误解。 我上面评论中列出的参考文献是否有帮助,为了清楚起见,在这里重复:std::basic_string::find 和 std::basic_string::擦除?


使用这三到四个修改,它应该可以解决问题。 请注意,上面帖子中的所有代码都重复修改和额外的注释,以便将它们全部放在一个位置以供将来查看。

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    string sample("hello world");
    cout << "The sample string is: ";
    cout << sample << endl;
    //erasing world
    cout << "Erasing world" << endl;
    //
    // Erase the word "world" from the string, while not sending erase more
    // characters to erase than going past the end of the string.
    // erase is friendly enough not to have issues with passing a higher count
    // in the second parameter, but future C++ versions could throw an exception
    // in some future standard, e.g. C++14 or later.
    //
    sample.erase(5, 6);
    cout << sample << endl;
    //finding h and erasing it
    //
    // Use the short version of the find member function to look for the string
    // "h".  iCharH is now declared as in int, instead of an iterator.  If the 
    // search string is not present, the iCharH value will be std::string::npos,
    // otherwise iCharH will contain the starting index position of the search
    // string.
    //    string::iterator iCharH = std::string::find(sample.begin(), sample.end(), "h");
    //
    int iCharH = sample.find("h");
    cout << "finding h and erasing it" << endl;
    //
    // Using a different overloaded form of erase, make sure to remove the 
    // exact number of characters.  In this case, the number is ONE.  The
    // std::string::npos can be shortened to string::npos, but only because
    // using namespace std; is above.
    //
    if (iCharH != std::string::npos){
        sample.erase(iCharH, 1);
    }
    cout << sample << endl;
    //erasing entirely
    //
    // Passing no parameters to erase empties a string.  This is optional, but
    // less typing than the iterator version.  The iterator version works 
    // perfectly too.
    //    sample.erase(sample.begin(), sample.end());
    //
    sample.erase();
    if (sample.length() == 0){
        cout << "The string is empty" << endl;
    }
    system("pause");
    return 0;
}

最新更新