C++ C++;字符串管理通过字典匹配转换原始字符串
这段代码的作用是,给定一个字符串,它将查看底部附加的数组,这样,如果一个字符串与其中一个数组元素匹配,字符串的所有新位将组合在一起形成一个新字符串,然后返回!换句话说,它通过在“dictionary”中查找来转换长的原始字符串,然后返回解释后的新strnig 我试图在//comment中添加一些代码,这样,如果遇到与记录中的任何内容都不匹配的原始字符串(如在数组中),那么它将调用另一个函数来处理它。我正在考虑把它记录下来,这样我就可以更新字典了 谢谢C++ C++;字符串管理通过字典匹配转换原始字符串,c++,C++,这段代码的作用是,给定一个字符串,它将查看底部附加的数组,这样,如果一个字符串与其中一个数组元素匹配,字符串的所有新位将组合在一起形成一个新字符串,然后返回!换句话说,它通过在“dictionary”中查找来转换长的原始字符串,然后返回解释后的新strnig 我试图在//comment中添加一些代码,这样,如果遇到与记录中的任何内容都不匹配的原始字符串(如在数组中),那么它将调用另一个函数来处理它。我正在考虑把它记录下来,这样我就可以更新字典了 谢谢 string string_look_up(
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 }
字符串查找(字符串数据)
{
字符串loc_string=“”;
std::stringstream ss(数据);
std::字符串行;
while(std::getline(ss,line,'0'))
{
if(line.empty())
{
继续;
}
不能为循环设置bool标志。如果找到匹配项,请将其设置为true。然后,可以检查注释所在的位置
if(flag)
{
//code if found
}
else
{
//code if not found
}
您可以使用std::find
查找数组中的原始字符串。要获取数字数组的索引,只需从std::find返回的迭代器中减去数组的开头
std::find需要两个迭代器(begin和end)作为搜索范围。普通指针可以用作迭代器(因为它们支持递增(++)和取消引用(*)。返回值是指向找到的第一个元素的interator或指向末尾的end迭代器(一个超过最后一个元素)
std::function参数允许您为原始字符串数组中找不到的行指定回调。您可以使用与签名匹配的任何函数或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;
}
您可以使用const auto it=translationTable.find(line);
搜索值,并将其与translationTable.end()
进行比较,以检查搜索是否成功。it->first
包含原始字符串(即“25643663”),而it->second
包含数字字符串(即“X”)那么……问题是什么?我不知道如何添加If/else检查以返回字典中没有的字符串,有什么想法吗?
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;
}
const std::map<std::string, std::string> translationTable = {
{"25643663", "X"},
{"2346442", "Y"},
{"2446442", "Y" }
// ...
};