1.string vs char*
//string to char* string str; const char* cch = str.c_str(); char *ch = new char[strlen(cch)+1]; strcpy(ch,cch); //char* to string string str = ch;
2.string vs CString
//CString to string CString cstr; string str = CStringA(cstr); //string to CString CString cstr = CString(str.c_str());
3.CString vs char*
//CString to char* CString cstr; int len = WideCharToMultiByte(CP_ACP, 0, cstr, -1, NULL, 0, NULL, NULL); char *ch = new char[len + 1]; WideCharToMultiByte(CP_ACP, 0, cstr, -1, ch, len, NULL, NULL); //char* to CString CString cstr = CString(ch);
4.int vs string
//采用c++11新增函數stoi,stof,stol,to_string等進行數值與string之間的轉換 //string to int string str = “111”; int i = stoi(str); //int to string string str = to_string(i);
本文只提供了很多種轉換方法中的一種,親測有效!