c++中,可以直接利用string及cout进行中文的存储及输出:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string s1="第一";
cout<<s1<<endl;
}正常输出:
第一
但是有些时候不得不用到wstring来存储中文字符,这时输出需要
- 导入locale头文件
- 中文字符前需要加L,并用wstring存储
- 输出前更改本地语言,wcout.imbue(locale("chs"))
- 用wcout输出
#include <iostream> #include <string> #include <locale> using namespace std; void main() { string s1="第一"; wstring s2=L"第二"; cout<<s1<<endl; wcout.imbue(locale("chs")); wcout<<s2<<endl; }
结果便是:
第一
第二
本文介绍了在C++中如何处理中文字符,包括使用string直接存储及输出中文的方法,以及在需要使用wstring存储中文时如何正确输出。文章通过示例代码展示了具体的实现步骤。
683

被折叠的 条评论
为什么被折叠?



