C++ 如何使用map对文本进行编码?
我试图以hello world文本为例进行编码。但在实施过程中有点迷失了方向C++ 如何使用map对文本进行编码?,c++,C++,我试图以hello world文本为例进行编码。但在实施过程中有点迷失了方向 const map<string_view, int64_t> map_hello { {"h", 199 },{"e", 112 },{"l", 103 },{"o", 109 },{"w", 190 },{"r", 115 },{"d", 162 } }; const map\u你好{ {“h”,199},{“e”,112},{“l”,103},{“o”,109},{“w”,190},{“r”
const map<string_view, int64_t> map_hello {
{"h", 199 },{"e", 112 },{"l", 103 },{"o", 109 },{"w", 190 },{"r", 115 },{"d", 162 }
};
const map\u你好{
{“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;
}
string txt=“你好世界”;
用于(自动i:txt){
cout您必须使用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;
}
intmain()
{
const std::map map_你好{
{“hel”,199},{“lo”,112},{“wor”,103},{“ld!”,109}
};
std::string txt=“你好,世界!”;
对于(int i=0;i std::cout我不太清楚你为什么使用std::string\u view
作为map的键。难道char
不合适吗?顺便说一句,字符是有限的,可以枚举的。一个简单的数组而不是std::map
也可以。你所说的“encode”到底是什么意思?@SamVarshavchik以数字的形式获取“hello world”一词,每个字符将接收一个唯一的代码。@Scheff将来我不想使用一个字符,而是一次使用3个字符,所以string.OK。这可能值得在您的问题中提及。请以防万一。之所以有字符串,是因为我可以使用专用字符和3-character words@user编辑为使用字符串,还添加了空格character@user如果你想要3个字符的单词,你必须改变地图和for循环,但是如何改变它是困难的?你的例子几乎适合我,可能关于3个字符的问题最好分开问。