我想在C++上显示一个“漂亮”的国家清单和他们的国际标准化组织货币代码。问题是,我的数据是法语,它有突出字符。这意味着阿尔及利亚,实际上是“阿尔吉里”,瑞典变成了“苏德”。
map<string, string> currencies; /* ISO code for currency and country name: CAD/Canada */
for (auto &cursor: countries)
cout << setw(15) << left << cursor.second << right << " - " cursor.first << endl;如果地图上包含阿尔及利亚、加拿大和瑞典,结果如下:
Canada - CAD
Algérie - DZD
Suède - SEK你看到阿尔及利亚和瑞典有多“漂亮”了吗?这是因为,尽管“阿格里”有7个可见字符,“苏爱”有5个,但它们“算作”了另一个词。"Algérie“中的"é”和"Suède“中的"è”是两个字符,因为它们是“特别强调的字符”。
是否有一种优雅的和简单的方法,以确保DZD和SEK自动与CAD保持一致?
发布于 2021-10-13 16:09:13
将std::string
std::wstring而不是std::wstring来使用宽字符串常量(L"stuff" vs 改为使用std::wcout而不是std::coutsetlocale来设置UTF-8区域设置wcout.imbue来配置UTF-8区域设置H 218g 219示例:
#include <map>
#include <string>
#include <iostream>
#include <iomanip>
#include <locale>
int main() {
setlocale(LC_ALL, "en_US.utf8");
std::locale loc("en_US.UTF-8");
std::wcout.imbue(loc);
std::map<std::wstring, std::wstring> dict
{ {L"Canada",L"CAD"}, {L"Algérie",L"DZD"}, {L"Suède",L"SEK"} };
for (const auto& [key, value]: dict) {
std::wcout << std::setw(10) << key << L" = " << value << std::endl;
}
} https://stackoverflow.com/questions/69558438
复制相似问题