【问题标题】:why can't i replace char on a string like this?为什么我不能在这样的字符串上替换字符?
【发布时间】:2018-05-27 19:16:08
【问题描述】:

我正在尝试替换以下映射中的字符:const map<char, vector<char>> ass,注意我有此字符串 pas,并且我想将所有(映射值)向量字符替换为对应的映射键,我尝试迭代用这样的for循环映射:(我从stackoverflow上的另一个问题得到这段代码)

for (auto const &ent1 : ass) {

    //ent1.first = first key
    //ent1.second = second key
}

所以我尝试像这样迭代地图值向量:

string char1;
string char2;
string wr;

for (auto const &ent1 : ass) {

    for (int i = 0; i < ent1.second.size(); i++) {

        specialValues += ent1.second[i];

        char2 = ent1.second[i];
        char1 = ent1.first;

        regex e("([" + char1 + "])");

        cout << ("([" + char1 + "])");
        cout << char2;

        wr = regex_replace("c1a0", e, char2);
    }

}

所以我希望字符串“c1a0”在循环之后变成“ciao”,但它不会改变任何东西,

我也试过了:

wr = regex_replace("c1a0", e, "o");

输出:c1a0

regex e("([0])");
wr = regex_replace("c1a0", e, char2);

输出:c1a2

我不知道,这对我来说毫无意义。我不明白,你能帮我弄清楚我的代码有什么问题吗?

当然,如果我写的话:

regex e("([0])");
wr = regex_replace("c1a0", e, "o");

它给了我“c1ao”,这就是我想要的。

【问题讨论】:

标签: c++ regex dictionary vector replace


【解决方案1】:

以下代码对我有用:

#include <string>
#include <map>
#include <regex>
#include <iostream>

using namespace std;

int main() {
    const map<char, vector<char>> ass = {
        { '1', {'i'} },
        { '0', {'o'} },
    };

    string char1;
    string char2;
    string wr = "c1a0";

    for (auto const &ent1 : ass) {

        for (int i = 0; i < ent1.second.size(); i++) {

            //specialValues += ent1.second[i];

            char2 = ent1.second[i];
            char1 = ent1.first;

            regex e("([" + char1 + "])");

            cout << ("([" + char1 + "])") << std::endl;
            cout << char2<< std::endl;

            wr = regex_replace(wr, e, char2);
            cout << wr << std::endl;

        }

    }
}

但是恕我直言,这里的正则表达式有点矫枉过正。您可以手动迭代字符串并替换字符,如下面的 sn-p:

#include <string>
#include <set>
#include <iostream>
#include <vector>

using namespace std;

struct replace_entry {
    char with;
    std::set<char> what;
};

int main() {
    const std::vector<replace_entry> replaceTable = {
        { 'i', {'1'} },
        { 'o', {'0'} },

    };

    string input = "c1a0";

    for (auto const &replaceItem : replaceTable) {
        for (char& c: input ) {
            if(replaceItem.what.end() != replaceItem.what.find(c)) {
                c = replaceItem.with;
            }
        }

    }
    cout << input << std::endl;
}

另一种方法是创建 256 个元素的字符数组

#include <string>
#include <iostream>

class ReplaceTable {
private:
    char replaceTable_[256];

public:
    constexpr ReplaceTable() noexcept 
        : replaceTable_()
    {
        replaceTable_['0'] = 'o';
        replaceTable_['1'] = 'i';
    }

    constexpr char operator[](char what) const noexcept {
        return replaceTable_[what];
    }

};

// One time initialization
ReplaceTable g_ReplaceTable;

int main() {

    std::string input = "c1a0";

    // Main loop
    for (char& c: input ) {
        if(0 != g_ReplaceTable[c] ) c = g_ReplaceTable[c];
    }

    std::cout << input << std::endl;
}

【讨论】:

  • 好吧,我只是愚蠢地将 char1 与 char2 混淆了,谢谢您的多种解决方案回答 btw
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多