【问题标题】:C++ STL string eraseC++ STD 字符串擦除
【发布时间】:2014-04-08 20:06:40
【问题描述】:

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

错误 2 错误 C2040:“==”:“int”的间接级别不同 来自 'const char [4]' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility line:3026 column:1 STL string erase

和

错误 1 ​​错误 C2446:“==”:没有从“const char *”转换为 'int' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility line:3026 column:1 STL string erase

这也是我的代码

#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::find 只找到单个元素(即字符),而不是子集(即子字符串)。使用std::string::find。
  • 转至此参考:std::basic_string::find,了解有关 std::string::find 成员函数的四个重载版本的详细信息。将此用于三种不同的擦除重载:std::basic_string::erase.
  • 用'h'替换string::iterator iCharH = std::string::find(sample.begin(), sample.end(), "h");中的"h"

标签: c++ string stl runtime-error


【解决方案1】:

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

【讨论】:

  • 一位高声誉会员(例如 100K+)告诉我,我在 30 多天前发布的一个答案中,上述网站不如cppreference.com 受欢迎。不幸的是,这是我从高声誉会员那里得到的唯一评论。我没有任何具体的理由避开所列网站。
  • 我确实尝试过,但它给了我一个错误。错误 1 ​​错误 C2665: 'std::basic_string,std::allocator>::find' : 4 个重载都不能转换所有参数类型 line:15 列: 1 STL 字符串擦除
  • @Jim 用于重新设计的words.find(...) 和words.erase(...) 函数调用的具体实现编码是什么?
  • @CPlusPlusOOAandD 我已经对我的帖子进行了更改,这是对上一篇的重新编码
  • @Jim 我已经发布了一个应该澄清的答案,并重复了方便的在线 C++ 参考页面。
【解决方案2】:

我认为对std::string::erase 和std::string::find 成员函数存在误解。我在上面的评论中列出的参考资料是否有帮助,为了清楚起见,在此重复:std::basic_string::find 和 std::basic_string::erase?


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

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

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多