C++字符串管理通过字典匹配转换原始字符串



此代码所做的是,给定一个字符串,它将查看底部附加的数组,这样,如果一个字符串与其中一个数组元素匹配,则该字符串的所有新位将组合在一起,形成一个新字符串,然后返回!换句话说,它通过在"dictionary"中查找一个长的原始字符串来转换它,然后返回解释的新strnig。

我正试图在//comment中添加一些代码,这样,如果我遇到与记录中的任何内容都不匹配的原始字符串,比如在数组中,它就会调用另一个函数来处理它。我正在考虑将其记录下来,以便更新字典。

谢谢!

string string_look_up(string data)
{
string loc_string = "";
std::stringstream ss(data);
std::string line;
while (std::getline(ss, line, '0'))
{
    if (line.empty())
    {
        continue;
    }
    cout << line << endl;
    for (int i = 0; i < 82; i++)
    {
        if (line == dictionary_array_strings_raw[i])
        {
            loc_string = loc_string + dictionary_array_strings_digits[i];
        }
    }
    /// this is where the new code I want should be
    cout << loc_string << endl;
    cout << "######" << endl;
}
return loc_string;
}
const string dictionary_array_strings_raw[] = { // x
                                                "25643663",
                                                // Y
                                                "2346442", "2446442",
                                                // Z
                                                "3676764",
                                                // :
                                                "4",
                                                // -
                                                "111" }
const string dictionary_array_strings_digits[] = { // x
                                                   "X",
                                                   // Y
                                                   "Y", "Y",
                                                   // Z
                                                   "Z",
                                                   // :
                                                   ":",
                                                   // -
                                                   "-",
                                                   // 1  }

您可以使用std::find在数组中定位原始字符串。要获得数字数组的索引,只需从std::find返回的迭代器中减去数组的开头。

std::find需要两个迭代器(begin和end)作为搜索范围。纯指针可以用作迭代器(因为它们支持递增(++)和取消引用(*))。返回值要么是指向找到的第一个元素的interator,要么是指向末尾(最后一个元素后面的一个)的end迭代器。

函数参数允许您为原始字符串数组中找不到的行指定回调。您可以使用任何与签名匹配的函数或lambda(const std::string&参数和std::string返回值)。

警告:以下代码不正确。它使用82表示字典条目的数量(就像原始帖子中的代码一样),但它只定义了6个字典条目。如果使用未知的原始字符串,这将导致未定义的行为。您应该添加76个字典条目,或者将条目数从82个减少到6个。

#include <string>
#include <map>
#include <sstream>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cassert>
using namespace std;
const string dictionary_array_strings_raw[] = { // x
    "25643663",
    // Y
    "2346442", "2446442",
    // Z
    "3676764",
    // :
    "4",
    // -
    "111"
};
const string dictionary_array_strings_digits[] = { // x
    "X",
    // Y
    "Y", "Y",
    // Z
    "Z",
    // :
    ":",
    // -
    "-",
    // 1  
};
string string_look_up(string data, std::function<std::string(const std::string&)> callback)
{
    assert(callback != nullptr);
    const auto dictionary_array_strings_raw_end = dictionary_array_strings_raw + 82;
    std::string loc_string = "";
    std::stringstream ss(data);
    std::string line;
    while (std::getline(ss, line, '0'))
    {
        if (line.empty())
        {
            continue;
        }
        cout << line << endl;
        const auto it = std::find(dictionary_array_strings_raw, dictionary_array_strings_raw_end, line);
        if (it == dictionary_array_strings_raw_end)
            loc_string += callback(line);
        else
            loc_string += dictionary_array_strings_digits[it - dictionary_array_strings_raw];
        cout << loc_string << endl;
        cout << "######" << endl;
    }
    return loc_string;
}

以下示例显示了对未知原始字符串(示例中为"99")使用lambda。

int main(int argc, char **argv)
{
    const auto result = string_look_up("25643663023464420990", [](const std::string& line) { return '<' + line + '>'; });
    std::cout << "Result:n" << result;
    return 0;
}

当string_look_up遇到包含"99"的行时,它会尝试查找匹配的原始字符串。当它找不到匹配的原始字符串时,它会调用以"99"作为唯一参数的回调函数。main中定义的lambda添加了"<"before和'>'并将结果返回给string_look_up(),后者将返回值添加到结果中。

输入25643663023464420990导致输出XY<99>(以及大量调试输出)

我建议对转换表使用std::map(原始字符串作为键,数字作为值)。对于映射,您可以使用find()成员函数来获取迭代器(让我们称之为"it"),并使用it->second来获取数字字符串。

以下是地图静态初始化的示例:

const std::map<std::string, std::string> translationTable = {
    {"25643663", "X"},
    {"2346442", "Y"},
    {"2446442", "Y" }
    // ...
};

您可以使用const auto it = translationTable.find(line);搜索值,并将其与translationTable.end()进行比较,以检查搜索是否成功。it->first包含原始字符串(如"25643663"),而it->second包含数字字符串(即"X")

为循环设置一个bool标志。如果找到匹配项,请将其设置为true。然后,你可以在你的评论所在的地方进行检查。

if(flag)
{
//code if found
}
else
{
//code if not found
}

最新更新