此數據類型主要解決漢字的問題吧。
理論方面我轉載其他優秀博主的博客,我這里就直接貼代碼講解具體使用吧,
另外,寬字符錄入(輸入)的問題,我找遍全網也沒見得可以用的,
確實是有wscanf函數,但是沒有函數使用樣例和報錯解決辦法,實在讓人為難,於是自己去翻函數庫一個個找,,,才找到(原創不易啊)
另外,所用函數是vs提供,所以可能會有其他編譯器無法使用的情況,故特別補充非vs編譯器可以使用的版本如下:
(選用Dev為樣例)

#include<iostream> #include<cstdio> #include<locale.h> #include<windows.h> using namespace std; int main() { _wsetlocale(LC_ALL, L"chs");//或者 setlocale(LC_ALL, "chs"); //若刪除上句,仍能輸出整個漢字語句,但不能單獨 使用/輸出 一個漢字 wchar_t s[5][81]; /*方式1 wcin >> s; wcout << s << endl; */ /*方式2 _getws(s); wprintf(L"%ls\n", s); */ system("pause>nul"); return 0; }
vs代碼如下:

#include<iostream> #include<locale.h> #include<stdio.h> #include<Windows.h> using namespace std; locale loc("chs"); /* //語句需要: #include<locale.h> locale loc("chs"); _wsetlocale(LC_ALL, L"chs");//或者setlocale(LC_ALL, "chs"); wcout.imbue(loc); */ int main() { _wsetlocale(LC_ALL, L"chs");//或者setlocale(LC_ALL, "chs"); wcout.imbue(loc); wchar_t s[] = L"我愛你"; wchar_t S[] = L"你愛我嗎"; /* _getws_s(s);//輸入類型1 wcin >> s;//輸入類型2 */ wprintf(L"%ls\n", S); wprintf(L"%s\n", S); printf("%S\n", S); /* %s 和 %S 輸出區別是: %s窄字符輸出, %ls寬字符輸出, %S輸出與輸出函數相反寬度的字符格式 (也就是printf因為默認支持的寬度是單字符,而%S偏要使用相反的,那么就 使用寬字符格式輸出,而wprintf默認支持的寬度是寬字節,%S偏要使用相反的, 意思就是使用單字符格式輸出) */ system("pause>nul");//輸出類型1 wcout << s << endl; system("pause>nul");//輸出類型2 putwchar(s[2]);//或者printf("%lc",s[2]); wcout <<'\n'<< S[0] << endl; puts((s[2] == S[0])?"yes":"no"); system("pause>nul");//簡單理解使用 return 0; }
詳細寬字符及其衍生函數:https://www.cnblogs.com/lanhaicode/p/10574260.html
_wsetlocale(LC_ALL, L"chs")//setlocale(LC_ALL, "chs")簡單說明及char與wchar_t轉換:https://www.cnblogs.com/zplutor/archive/2010/11/27/1889227.html
路過點個贊?