如何在C++;? 我是C++新手。我在C++入门第五版(EX 9.43)中遇到了一个问题。for循环无法在我的函数find_和_replace中停止。代码如下: #include <iostream> #include <string> int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace); int main(int argc, char const *argv[]) { std::string str("I am a very loooooong string to be process!"); int find_times; find_times = find_and_replace(str, "a", "###"); std::cout << find_times << std::endl; std::cout << str << std::endl; return 0; } int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace) { int find_times = 0; if (org_str.size() < str4find.size()) { std::cout << "Error: The original string is too short!" << std::endl; return find_times; } else if (org_str == str4find) { org_str.assign(str4replace); ++find_times; return find_times; } for (auto i = org_str.begin(), j = i + str4find.size(); j != org_str.end(); ) { std::string temp(i, j); // std::cout << temp << std::endl; if (temp == str4find) { j = org_str.erase(i, j); org_str.insert(i, str4replace.begin(), str4replace.end()); // std::cout << org_str << std::endl; // std::cout << *i << std::endl; j = i + str4find.size(); // std::cout << *j << std::endl; ++find_times; } else { ++i; ++j; } } if (org_str.substr(org_str.size() - str4find.size()) == str4find) { org_str.erase(org_str.size() - str4find.size(), str4find.size()); org_str.insert(org_str.end(), str4replace.begin(), str4replace.end()); ++find_times; } return find_times; } #包括 #包括 int find_和_replace(std::string和org_str,const std::string和str4find,const std::string和str4replace); int main(int argc,char const*argv[] { std::string str(“我是一个需要处理的非常长的字符串!”); 整数次; find#u times=查找并替换(str,“a”,“####”); std::cout

如何在C++;? 我是C++新手。我在C++入门第五版(EX 9.43)中遇到了一个问题。for循环无法在我的函数find_和_replace中停止。代码如下: #include <iostream> #include <string> int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace); int main(int argc, char const *argv[]) { std::string str("I am a very loooooong string to be process!"); int find_times; find_times = find_and_replace(str, "a", "###"); std::cout << find_times << std::endl; std::cout << str << std::endl; return 0; } int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace) { int find_times = 0; if (org_str.size() < str4find.size()) { std::cout << "Error: The original string is too short!" << std::endl; return find_times; } else if (org_str == str4find) { org_str.assign(str4replace); ++find_times; return find_times; } for (auto i = org_str.begin(), j = i + str4find.size(); j != org_str.end(); ) { std::string temp(i, j); // std::cout << temp << std::endl; if (temp == str4find) { j = org_str.erase(i, j); org_str.insert(i, str4replace.begin(), str4replace.end()); // std::cout << org_str << std::endl; // std::cout << *i << std::endl; j = i + str4find.size(); // std::cout << *j << std::endl; ++find_times; } else { ++i; ++j; } } if (org_str.substr(org_str.size() - str4find.size()) == str4find) { org_str.erase(org_str.size() - str4find.size(), str4find.size()); org_str.insert(org_str.end(), str4replace.begin(), str4replace.end()); ++find_times; } return find_times; } #包括 #包括 int find_和_replace(std::string和org_str,const std::string和str4find,const std::string和str4replace); int main(int argc,char const*argv[] { std::string str(“我是一个需要处理的非常长的字符串!”); 整数次; find#u times=查找并替换(str,“a”,“####”); std::cout,string,c++11,for-loop,iterator,String,C++11,For Loop,Iterator,调用erase和insert后,i迭代器无效,您应该使用该函数的返回值,或者重新计算从s.begin()开始的位置 #include <iostream> #include <string> using std::cout; using std::string; int find_and_replace( string &s, const string &oldVal, const string &newVal ) { int find

调用erase和insert后,
i
迭代器无效,您应该使用该函数的返回值,或者重新计算从
s.begin()开始的位置

#include <iostream>
#include <string>

using std::cout;
using std::string;

int find_and_replace( string &s, const string &oldVal, const string &newVal ) {
    int find_times = 0;
    if ( s.size() < oldVal.size() ) {
        std::cout << "Error: The original string is too short!" << std::endl;
        return find_times;
    }
    if ( s == oldVal ) {
        s.assign(newVal);
        return ++find_times;
    }
    for ( auto i = s.begin(); i != s.end(); ++i ) {
        // compare using iterators
        bool is_equal = true;
        auto k = i;
        for ( auto j = oldVal.begin(); j != oldVal.end(); ++j, ++k ) {
            if ( k == s.end() || *j != *k ) {
                is_equal = false;
                break;
            }
        }
        if ( is_equal ) {
            auto ie = s.erase(i, i + oldVal.size());
            // This may not work with older version of g++
            auto ii = s.insert(ie, std::begin(newVal), std::end(newVal));
            i = ii + newVal.size() - 1;
            ++find_times;
        }
    }
    return find_times;
}

int main() {
    string str("I am a very loooooong string to be process!");
    int find_times;
    find_times = find_and_replace(str, "a", "###");
    cout << find_times << '\n';
    cout << str << '\n';

    return 0;
}

org\u str.insert
之后,
i
j
是无效的。你永远不会使
i
再次有效,你更新了
j
相对于无效的
i
@MikeKinghan当时,我使用std::cout来显示*i和*j,输出是正确的,所以我认为我和j都是有效的。我犯了错误。是的,你是对的在开始时,我这样写:I=org_str.insert(org_str.end(),str4replace.begin(),str4replace.end());但是,我的编译器告诉我org_str.insert(…)的返回值是空的。因此,我使用cout检查I是否无效。输出的*I是正确的,因此我认为I始终有效。现在,我将其更改为for循环:for(auto k=str4replace.begin();k!=str4replace.end();++k,++i){i=org_str.insert(i,*k)}一切顺利!
#include <iostream>
#include <string>

using std::cout;
using std::string;

int find_and_replace( string &s, const string &oldVal, const string &newVal ) {
    int find_times = 0;
    if ( s.size() < oldVal.size() ) {
        std::cout << "Error: The original string is too short!" << std::endl;
        return find_times;
    }
    if ( s == oldVal ) {
        s.assign(newVal);
        return ++find_times;
    }
    for ( auto i = s.begin(); i != s.end(); ++i ) {
        // compare using iterators
        bool is_equal = true;
        auto k = i;
        for ( auto j = oldVal.begin(); j != oldVal.end(); ++j, ++k ) {
            if ( k == s.end() || *j != *k ) {
                is_equal = false;
                break;
            }
        }
        if ( is_equal ) {
            auto ie = s.erase(i, i + oldVal.size());
            // This may not work with older version of g++
            auto ii = s.insert(ie, std::begin(newVal), std::end(newVal));
            i = ii + newVal.size() - 1;
            ++find_times;
        }
    }
    return find_times;
}

int main() {
    string str("I am a very loooooong string to be process!");
    int find_times;
    find_times = find_and_replace(str, "a", "###");
    cout << find_times << '\n';
    cout << str << '\n';

    return 0;
}
2

I ###m ### very loooooong string to be process!