string,wstring,cout,wcout 与中文字符的输入输出 .
来源:互联网 发布:知我者,二三子 编辑:程序博客网 时间:2024/05/02 00:32
转自:http://blog.csdn.net/efeics/article/details/8044014
c++中,可以直接利用string及cout进行中文的存储及输出:
- #include <string>
- using namespace std;
- void main()
- {
- string s1="第一";
- cout<<s1<<endl;
- }
#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;
- }
#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.
不知道还有木有其他区别?
- string,wstring,cout,wcout 与中文字符的输入输出
- string,wstring,cout,wcout 与中文字符的输入输出 .
- string,wstring,cout,wcout 与中文字符的输入输出
- string,wstring,cout,wcout 与中文字符的输入输出
- cout、wcout无法正常输出中文字符问题的深入调查(1):各种编译器测试
- cout与wcout
- cout 与wcout
- [C++基础]cout与wcout,printf与wprintf,ofstream与wofstream关于输出中文的问题解决
- cout wcout ifstream 处理中文时的问题
- cout,wcout,printf,wprintf等函数的中文输出问题
- string 与 wstring 的中文互转(zt)
- wstring 与 string 的转换
- 用wcout输出有汉字的wstring
- 用wcout输出有汉字的wstring
- MinGW-W64使得printf、cout、wprintf、wcout显示出中文的种种
- wcout / wstring / wchar_t
- string与wstring的互转
- wstring 与 string 之间的转换
- sizeof ()那点事
- NYOJ125---盗梦空间
- pinyin4j使用
- http://poj.grids.cn/practice/2746--约瑟夫问题
- EOS条件查询
- string,wstring,cout,wcout 与中文字符的输入输出 .
- 深刻理解Python中的元类
- C++中delete和delete[]的区别
- startActivityForResult
- Spatial Pyramid Matching 小结
- RAC 修改实例名
- Android AlarmManager实现不间断轮询服务
- HDU 1116 Play on Words (欧拉回路)
- objective-c 字符串基本操作