string,wstring,cout,wcout 与中文字符的输入输出 .

转自:http://blog.csdn.net/efeics/article/details/8044014

 

c++中,可以直接利用string及cout进行中文的存储及输出:


#include <iostream>   
  1. #include <string>   
  2. using namespace std;  
  3.   
  4. void main()  
  5. {  
  6.     string s1="第一";  
  7.     cout<<s1<<endl;   
  8. }  
#include <iostream>
#include <string>
using namespace std;

void main()
{
	string s1="第一";
	cout<<s1<<endl;	
}

正常输出:

第一

但是有些时候不得不用到wstring来存储中文字符,这时输出需要

  • 导入locale头文件
  • 中文字符前需要加L,并用wstring存储
  • 输出前更改本地语言,wcout.imbue(locale("chs"))
  • 用wcout输出
  1. #include <iostream>   
  2. #include <string>   
  3. #include <locale>   
  4. using namespace std;  
  5.   
  6. void main()  
  7. {  
  8.     string s1="第一";  
  9.     wstring s2=L"第二";  
  10.     cout<<s1<<endl;  
  11.     wcout.imbue(locale("chs"));  
  12.     wcout<<s2<<endl;  
  13. }  
#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;
}

结果便是:

第一

第二

 

 

个人看法:

wstring 返回“第二”的size为2.

 如果是string,返回的size为4.

不知道还有木有其他区别?

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值