C++中寬字符類型的定義及使用

1. 寬字符類型的定義
    寬字符類型 wchar_t 是這樣來的:

typedef short int wchar_t;

    所以 wchar_t 實際上的空間是和 short int 一樣,佔兩個字節。
    寬字符類型wchar_t常用來存儲中文、日文和韓文;
    使用前需包含頭文件,並聲明語言區域:

#include<locale>
setlocale(LC_ALL, "chs");//指定字符區域爲中文

 2. 下面給出在VS2010,win32環境下寬字符類型和單字符類型的比較

#include<iostream>
#include<locale>
using namespace std;
int main()
{
 setlocale(LC_ALL, "chs");//指定字符區域爲中文
 wchar_t st[] = L"中文的";
 char stc[] = "中文的";
 wcout<<st<<endl;//寬字符類型數組的內容:"中文的"
 cout<<stc<<endl;//單字符類型的內容:"中文的"
 cout<<st<<endl;//寬字符類型數組的地址
 cout<<&stc<<endl;//單字符類型的地址
 cout<<sizeof(st)<<endl;//寬字符類型數組的大小:3*2+2 = 8;
 cout<<sizeof(stc)<<endl;//單字符類型數組的大小:6*1+1 = 7;中文字符以兩個單字符存儲
 cout<<"------------------------------"<<endl;
 cout<<sizeof(char)<<endl;
 cout<<sizeof(wchar_t)<<endl;
 return 0;
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章