需要帮助在C ++中擦除和插入

时间:2013-09-23 20:23:38

标签: c++ insert erase

所以基本上我想创建一个接受字符串的格式函数,并用该用户想要替换的内容替换该字符串中的单词。起初我遇到了一些非可分解迭代器的问题,直到我意识到当你改变字符串的大小时,你可以使任何迭代器无效。它现在不再抛出异常现在输出与输入相同。任何建议???

string& formatFn(string& s, string& oldWord, string& newWord)
{
    string word = "";
    for (auto iter1 = s.begin(); iter1 != s.end(); ++iter1)
    {       
            string tmpWord = "";                         
        if (!isblank(*iter1))       // Testing for whitespace
        {
            tmpWord += *iter1;
            if (tmpWord == oldWord)
            {
                string::iterator beg = iter1 - word.size();
                string::iterator end = iter1;
                auto sIter = s.erase(beg, end);                 // Get the position returned by erase
                auto i = sIter - s.begin();                     // Get an index 
                s = s.insert(s[i], newWord);
            }
        }
        if (isblank(*iter1))
        {
            tmpWord.clear();
        }
    }
    return s;
}

3 个答案:

答案 0 :(得分:0)

string::iterator beg = iter1 - word.size();

我不确定word究竟做了什么。您正试图删除oldWord,对吧?那应该是:

string::iterator beg = iter1 - oldWord.size();

编辑:这是您代码的改进版本:

string formatFn(const string& s, const string& oldWord, const string& newWord) {
    string result = "";     // holds the string we want to return
    string word = "";       // while iterating over 's', holds the current word

    for (auto iter1 = s.begin(); iter1 != s.end(); ++iter1)  {       
        if (!isblank(*iter1))
            word += *iter1;
        else {    // if it is a whitespace, it must be the end of some word
            // if 'word' is not same as 'oldword', just append it
            // otherwise append 'newWord' instead

            if (word == oldWord)
                result += newWord;
            else
                result += word;
            result += *iter1;
            word.clear();    // reset 'word' to hold the next word in s
        }
    }

    // the end of the string might not end with a whitespace, so the last word
    // might be skipped if you don't make this test

    if (word == oldWord)
        result += newWord;
    else
        result += word;

    return result;
}

答案 1 :(得分:0)

如果您已使用字符串,为什么不使用所有方法?

for (auto it = text.find(o_text); it != string::npos; it = text.find(o_text)){
    text.replace(it, o_text.size(), n_text);
}

答案 2 :(得分:0)

你过于复杂了:

std::string replace_all(std::string s, const std::string& sOld, const std::string& sNew)
{
    std::size_t p = s.find(sOld);
    while (p != std::string::npos)
    {
        s.replace(p, sOld.length(), sNew);
        p = s.find(sOld, p + sNew.length());
    }
    return s;
}

如果您只想替换整个单词(您当前的尝试不会这样做):

#include <iostream>
#include <string>

bool test(const std::string& s, const std::string& sOld, std::size_t pos)
{
    return (pos == 0 || !::isalpha(s[pos - 1])) && (!::isalpha(s[pos + sOld.length()]) || pos + sOld.length() >= s.length());
}

std::size_t find_word(const std::string& s, const std::string& sOld, std::size_t pos)
{
    pos = s.find(sOld, pos);
    while (pos != std::string::npos && (!test(s, sOld, pos) && pos < s.length()))
    {
        pos++;
        pos = s.find(sOld, pos);
    }
    return pos;
}

std::string replace_all(std::string s, const std::string& sOld, const std::string& sNew)
{
    std::size_t p = find_word(s, sOld, 0);
    while (p != std::string::npos && p < s.length()) 
    {
        s.replace(p, sOld.length(), sNew);
        p = find_word(s, sOld, p + sNew.length()); 
    }
    return s;
}

int main() 
{
    std::string sOrig = "eat Heat eat beat sweat cheat eat";
    std::string sOld = "eat";
    std::string sNew = "ate";
    std::string sResult = replace_all(sOrig, sOld, sNew);
    std::cout << "Result:  " << sResult << std::endl;
    // Output:  "ate Heat ate beat sweat cheat ate"
    return 0;
}