英文:
Can this JSON be turned into a std::map?
问题
以下是您要的JSON数据的std::map<CString, CString>
形式,以便您可以根据#xxxxxx
的代码查找颜色描述。这将帮助您在应用程序中构建HTML语法。
#include <map>
#include <string>
std::map<CString, CString> colorMap = {
{_T("#F0F8FF"), _T("AliceBlue")},
{_T("#FAEBD7"), _T("AntiqueWhite")},
{_T("#00FFFF"), _T("Aqua")},
// 继续添加其他颜色数据...
};
// 要查找颜色描述,可以使用colorMap,例如:
CString colorCode = _T("#ff0000"); // 想要查找的颜色代码
auto it = colorMap.find(colorCode);
if (it != colorMap.end()) {
CString colorDescription = it->second;
// 使用colorDescription进行进一步操作,比如构建HTML语法
}
这将为您提供一个colorMap
,其中包含颜色代码和对应的颜色描述。您可以通过查找颜色代码来获取颜色描述。如果找到匹配,您可以进一步使用颜色描述来构建HTML语法或进行其他操作。
英文:
Sample JSON:
[
{
"Hex": "#F0F8FF",
"Colour": "AliceBlue"
},
{
"Hex": "#FAEBD7",
"Colour": "AntiqueWhite"
},
{
"Hex": "#00FFFF",
"Colour": "Aqua"
},
{
"Hex": "#7FFFD4",
"Colour": "Aquamarine"
},
{
"Hex": "#F0FFFF",
"Colour": "Azure"
},
{
"Hex": "#F5F5DC",
"Colour": "Beige"
},
{
"Hex": "#FFE4C4",
"Colour": "Bisque"
},
{
"Hex": "#000000",
"Colour": "Black"
},
{
"Hex": "#FFEBCD",
"Colour": "BlanchedAlmond"
},
{
"Hex": "#0000FF",
"Colour": "Blue"
},
{
"Hex": "#8A2BE2",
"Colour": "BlueViolet"
},
{
"Hex": "#A52A2A",
"Colour": "Brown"
},
{
"Hex": "#DEB887",
"Colour": "BurlyWood"
},
{
"Hex": "#5F9EA0",
"Colour": "CadetBlue"
},
{
"Hex": "#7FFF00",
"Colour": "Chartreuse"
},
{
"Hex": "#D2691E",
"Colour": "Chocolate"
},
{
"Hex": "#FF7F50",
"Colour": "Coral"
},
{
"Hex": "#6495ED",
"Colour": "CornflowerBlue"
},
{
"Hex": "#FFF8DC",
"Colour": "Cornsilk"
},
{
"Hex": "#DC143C",
"Colour": "Crimson"
},
{
"Hex": "#00FFFF",
"Colour": "Cyan"
},
{
"Hex": "#00008B",
"Colour": "DarkBlue"
},
{
"Hex": "#008B8B",
"Colour": "DarkCyan"
},
{
"Hex": "#B8860B",
"Colour": "DarkGoldenRod"
},
{
"Hex": "#A9A9A9",
"Colour": "DarkGrey"
},
{
"Hex": "#006400",
"Colour": "DarkGreen"
},
{
"Hex": "#BDB76B",
"Colour": "DarkKhaki"
},
{
"Hex": "#8B008B",
"Colour": "DarkMagenta"
},
{
"Hex": "#556B2F",
"Colour": "DarkOliveGreen"
},
{
"Hex": "#FF8C00",
"Colour": "Darkorange"
},
{
"Hex": "#9932CC",
"Colour": "DarkOrchid"
},
{
"Hex": "#8B0000",
"Colour": "DarkRed"
},
{
"Hex": "#E9967A",
"Colour": "DarkSalmon"
},
{
"Hex": "#8FBC8F",
"Colour": "DarkSeaGreen"
},
{
"Hex": "#483D8B",
"Colour": "DarkSlateBlue"
},
{
"Hex": "#2F4F4F",
"Colour": "DarkSlateGrey"
},
{
"Hex": "#00CED1",
"Colour": "DarkTurquoise"
},
{
"Hex": "#9400D3",
"Colour": "DarkViolet"
},
{
"Hex": "#FF1493",
"Colour": "DeepPink"
},
{
"Hex": "#00BFFF",
"Colour": "DeepSkyBlue"
},
{
"Hex": "#696969",
"Colour": "DimGray"
},
{
"Hex": "#1E90FF",
"Colour": "DodgerBlue"
},
{
"Hex": "#B22222",
"Colour": "FireBrick"
},
{
"Hex": "#FFFAF0",
"Colour": "FloralWhite"
},
{
"Hex": "#228B22",
"Colour": "ForestGreen"
},
{
"Hex": "#FF00FF",
"Colour": "Fuchsia"
},
{
"Hex": "#DCDCDC",
"Colour": "Gainsboro"
},
{
"Hex": "#F8F8FF",
"Colour": "GhostWhite"
},
{
"Hex": "#FFD700",
"Colour": "Gold"
},
{
"Hex": "#DAA520",
"Colour": "GoldenRod"
},
{
"Hex": "#808080",
"Colour": "Grey"
},
{
"Hex": "#008000",
"Colour": "Green"
},
{
"Hex": "#ADFF2F",
"Colour": "GreenYellow"
},
{
"Hex": "#F0FFF0",
"Colour": "HoneyDew"
},
{
"Hex": "#FF69B4",
"Colour": "HotPink"
},
{
"Hex": "#CD5C5C",
"Colour": "IndianRed"
},
{
"Hex": "#4B0082",
"Colour": "Indigo"
},
{
"Hex": "#FFFFF0",
"Colour": "Ivory"
},
{
"Hex": "#F0E68C",
"Colour": "Khaki"
},
{
"Hex": "#E6E6FA",
"Colour": "Lavender"
},
{
"Hex": "#FFF0F5",
"Colour": "LavenderBlush"
},
{
"Hex": "#7CFC00",
"Colour": "LawnGreen"
},
{
"Hex": "#FFFACD",
"Colour": "LemonChiffon"
},
{
"Hex": "#ADD8E6",
"Colour": "LightBlue"
},
{
"Hex": "#F08080",
"Colour": "LightCoral"
},
{
"Hex": "#E0FFFF",
"Colour": "LightCyan"
},
{
"Hex": "#FAFAD2",
"Colour": "LightGoldenRodYellow"
},
{
"Hex": "#D3D3D3",
"Colour": "LightGrey"
},
{
"Hex": "#90EE90",
"Colour": "LightGreen"
},
{
"Hex": "#FFB6C1",
"Colour": "LightPink"
},
{
"Hex": "#FFA07A",
"Colour": "LightSalmon"
},
{
"Hex": "#20B2AA",
"Colour": "LightSeaGreen"
},
{
"Hex": "#87CEFA",
"Colour": "LightSkyBlue"
},
{
"Hex": "#778899",
"Colour": "LightSlateGrey"
},
{
"Hex": "#B0C4DE",
"Colour": "LightSteelBlue"
},
{
"Hex": "#FFFFE0",
"Colour": "LightYellow"
},
{
"Hex": "#00FF00",
"Colour": "Lime"
},
{
"Hex": "#32CD32",
"Colour": "LimeGreen"
},
{
"Hex": "#FAF0E6",
"Colour": "Linen"
},
{
"Hex": "#FF00FF",
"Colour": "Magenta"
},
{
"Hex": "#800000",
"Colour": "Maroon"
},
{
"Hex": "#66CDAA",
"Colour": "MediumAquaMarine"
},
{
"Hex": "#0000CD",
"Colour": "MediumBlue"
},
{
"Hex": "#BA55D3",
"Colour": "MediumOrchid"
},
{
"Hex": "#9370D8",
"Colour": "MediumPurple"
},
{
"Hex": "#3CB371",
"Colour": "MediumSeaGreen"
},
{
"Hex": "#7B68EE",
"Colour": "MediumSlateBlue"
},
{
"Hex": "#00FA9A",
"Colour": "MediumSpringGreen"
},
{
"Hex": "#48D1CC",
"Colour": "MediumTurquoise"
},
{
"Hex": "#C71585",
"Colour": "MediumVioletRed"
},
{
"Hex": "#191970",
"Colour": "MidnightBlue"
},
{
"Hex": "#F5FFFA",
"Colour": "MintCream"
},
{
"Hex": "#FFE4E1",
"Colour": "MistyRose"
},
{
"Hex": "#FFE4B5",
"Colour": "Moccasin"
},
{
"Hex": "#FFDEAD",
"Colour": "NavajoWhite"
},
{
"Hex": "#000080",
"Colour": "Navy"
},
{
"Hex": "#FDF5E6",
"Colour": "OldLace"
},
{
"Hex": "#808000",
"Colour": "Olive"
},
{
"Hex": "#6B8E23",
"Colour": "OliveDrab"
},
{
"Hex": "#FFA500",
"Colour": "Orange"
},
{
"Hex": "#FF4500",
"Colour": "OrangeRed"
},
{
"Hex": "#DA70D6",
"Colour": "Orchid"
},
{
"Hex": "#EEE8AA",
"Colour": "PaleGoldenRod"
},
{
"Hex": "#98FB98",
"Colour": "PaleGreen"
},
{
"Hex": "#AFEEEE",
"Colour": "PaleTurquoise"
},
{
"Hex": "#D87093",
"Colour": "PaleVioletRed"
},
{
"Hex": "#FFEFD5",
"Colour": "PapayaWhip"
},
{
"Hex": "#FFDAB9",
"Colour": "PeachPuff"
},
{
"Hex": "#CD853F",
"Colour": "Peru"
},
{
"Hex": "#FFC0CB",
"Colour": "Pink"
},
{
"Hex": "#DDA0DD",
"Colour": "Plum"
},
{
"Hex": "#B0E0E6",
"Colour": "PowderBlue"
},
{
"Hex": "#800080",
"Colour": "Purple"
},
{
"Hex": "#FF0000",
"Colour": "Red"
},
{
"Hex": "#BC8F8F",
"Colour": "RosyBrown"
},
{
"Hex": "#4169E1",
"Colour": "RoyalBlue"
},
{
"Hex": "#8B4513",
"Colour": "SaddleBrown"
},
{
"Hex": "#FA8072",
"Colour": "Salmon"
},
{
"Hex": "#F4A460",
"Colour": "SandyBrown"
},
{
"Hex": "#2E8B57",
"Colour": "SeaGreen"
},
{
"Hex": "#FFF5EE",
"Colour": "SeaShell"
},
{
"Hex": "#A0522D",
"Colour": "Sienna"
},
{
"Hex": "#C0C0C0",
"Colour": "Silver"
},
{
"Hex": "#87CEEB",
"Colour": "SkyBlue"
},
{
"Hex": "#6A5ACD",
"Colour": "SlateBlue"
},
{
"Hex": "#708090",
"Colour": "SlateGrey"
},
{
"Hex": "#FFFAFA",
"Colour": "Snow"
},
{
"Hex": "#00FF7F",
"Colour": "SpringGreen"
},
{
"Hex": "#4682B4",
"Colour": "SteelBlue"
},
{
"Hex": "#D2B48C",
"Colour": "Tan"
},
{
"Hex": "#008080",
"Colour": "Teal"
},
{
"Hex": "#D8BFD8",
"Colour": "Thistle"
},
{
"Hex": "#FF6347",
"Colour": "Tomato"
},
{
"Hex": "#40E0D0",
"Colour": "Turquoise"
},
{
"Hex": "#EE82EE",
"Colour": "Violet"
},
{
"Hex": "#F5DEB3",
"Colour": "Wheat"
},
{
"Hex": "#FFFFFF",
"Colour": "White"
},
{
"Hex": "#F5F5F5",
"Colour": "WhiteSmoke"
},
{
"Hex": "#FFFF00",
"Colour": "Yellow"
},
{
"Hex": "#9ACD32",
"Colour": "YellowGreen"
}
]
I would like this as a std::map<CString, CString>
so that I can take a #xxxxxx
code and look it up in the map and if it is found, get the description of the colour. This is for ultimately building some HTML syntax in my application.
I know I can manually call map.emplace(L"#ff0000", L"red");
and have a big list of them. But is there another way?
答案1
得分: 3
以下是已翻译的内容:
这个JSON数据的格式已经类似于C++所需的格式。只需进行查找/替换,删除所有出现的 `"Hex":` 和 `Colour:`。最终会得到如下结果:
std::map<std::string, std::string> map = {
{
"#F0F8FF",
"AliceBlue"
},
{
"#FAEBD7",
"AntiqueWhite"
},
{
"#00FFFF",
"Aqua"
}
//...
};
你可以将其用作
printf("map: %s\n", map["#F0F8FF"].c_str());
//输出:map: AliceBlue
或者你可以反转这个映射
std::map<std::string, std::string> reverse_map;
for (const auto& p : map)
reverse_map.emplace(p.second, p.first);
printf("reverse_map %s\n", reverse_map["AliceBlue"].c_str());
//输出:reverse_map: #F0F8FF
希望这对你有所帮助。
英文:
The format for this JSON data is already similar to what c++ need. Just do a find/replace to remove all occurances of "Hex":
and Colour:
. You end up with something like this:
std::map<std::string, std::string> map = {
{
"#F0F8FF",
"AliceBlue"
},
{
"#FAEBD7",
"AntiqueWhite"
},
{
"#00FFFF",
"Aqua"
}
//...
};
You can use this as
printf("map: %s\n", map["#F0F8FF"].c_str());
//output: map: AliceBlue
Or you can reverse the map
std::map<std::string, std::string> reverse_map;
for (const auto& p : map)
reverse_map.emplace(p.second, p.first);
printf("reverse_map %s\n", reverse_map["AliceBlue"].c_str());
//output: reverse_map: #F0F8FF
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论