C++類的靜態成員初始化具體講授。本站提示廣大學習愛好者:(C++類的靜態成員初始化具體講授)文章只能為提供參考,不一定能成為您想要的結果。以下是C++類的靜態成員初始化具體講授正文
在處置中文時有時須要停止wchar_t,char,string,wstring之間的轉換。
個中char和string之間、wchar_t和wstring之間的轉換較為簡略,代碼在vs2010下測試經由過程。
#include <iostream>
#include <string>
#include <tchar.h>
#include <Windows.h>
using namespace std;
//Converting a WChar string to a Ansi string
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nlength=wcslen(pwstr);
//獲得轉換後的長度
int nbytes = WideCharToMultiByte( 0, 0, pwstr, nlength, NULL,0,NULL, NULL );
if(nbytes>len) nbytes=len;
// 經由過程以上獲得的成果,轉換unicode 字符為ascii 字符
WideCharToMultiByte( 0,0, pwstr, nlength, pcstr, nbytes, NULL, NULL );
return pcstr ;
}
int main(){
setlocale(LC_ALL,"chs");
char* cc = "this is a char 測試";
wchar_t* wcc = L"this is a wchar 測試";
string str("this is a string 測試 ");
wstring wstr = L"this is a wstring 測試";
//string to char
const char* char_test = str.c_str();
//cout<<"char_test:"<<char_test<<endl;
//char to string
string ss = cc;
//cout<<"ss is :"<<ss<<endl;
//wstring to wchar
const wchar_t* wchar_test = wstr.c_str();
//wcout<<wchar_test<<endl;
//wchar to wstring
wstring wss = wcc;
wcout<<wcc<<endl;
//char to wchar_t
wchar_t *wc = new wchar_t[str.size()+1];
//swprintf(wc,L"%S",cc);
//wcout<<cc<<endl;
delete []wc;
// wchar_t to char
char *pcstr = (char *)malloc(sizeof(char)*(2 * wcslen(wcc)+1));
memset(pcstr , 0 , 2 * wcslen(wcc)+1 );
w2c(pcstr,wcc,2 * wcslen(wcc)+1) ;
free(pcstr);
system("pause");
return 1;
}