我刚刚下载了 Visual studio 2019,但我遇到了字符串编码问题。 这是我的 C++ 代码:
cout << "èà" << "\n" << endl;
string str;
cout << "Write something: "
cin >> str;
cout << str << endl
第一行的输出是:
èà
如果我在第 5 行输入“èà”,则输出为:
èà
我不明白为什么。你能帮我吗?
源文件编码是UTF-8,我已经查过了。
请具体回答,因为我刚刚开始使用 Visual Studio。
STD C++ 20 解决方案:
#include <iostream>
#include <string>
#ifdef _WIN32
#include <windows.h>
#include <clocale>
#endif
int main() {
#ifdef _WIN32
// console UTF-8
std::setlocale(LC_CTYPE, ".UTF8");
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
std::string str = "𨉟呐㗂越 🤑 αβγδ ñ";
std::cout << str << std::endl;
return 0;
}
MSVC 需要以下标志:
set(CMAKE_CXX_STANDARD 20)
target_compile_options(hello_world PRIVATE /source-charset:utf-8 /execution-charset:utf-8)
大多数功能不再需要使用 u8string。