如何使用地图对文本进行编码?

How to encode text using map?

本文关键字:编码 文本 何使用 地图      更新时间:2023-10-16

我正在尝试编码hello world文本作为示例。但是在实施过程中有点迷失。

const map<string_view, int64_t> map_hello {
{"h", 199 },{"e", 112 },{"l", 103 },{"o", 109 },{"w", 190 },{"r", 115 },{"d", 162 }
};

我在这里迷路了,我不知道下一步该怎么做,如何比较字符并将数字转移给他们。

string txt = "hello world";
for (auto i : txt) {

cout << i << " result " << endl;
}

您必须使用at()方法搜索密钥的相应值std::map

int main()
{
const std::map<std::string_view, int64_t> map_hello {
{"hel", 199 },{"lo ", 112 },{"wor", 103 },{"ld!", 109 }
};
std::string txt = "hello world!";
for(int i = 0; i < txt.size(); i += 3) {
auto s1 = txt.substr(i, 3);
std::cout << s1 << std::endl;
auto s2 = map_hello.at(s1);
std::cout << s2 << " result " << std::endl;
}
return 0;
}