
本章内容给大家谈谈关于遇上c++编程之cstring、string与char数组怎么进行转换等问题,我们该怎么处理呢。下面这篇文章将为你提供一个解决思路,希望能帮你解决到相关问题。
CString与string的转换
1、CString转换为string:
// 使用C++标准库
CString cstr = _T("Hello World");
std::string str = (LPCTSTR)cstr;
// 使用MFC
CString cstr = _T("Hello World");
CStringA str(cstr);
std::string str = (LPCSTR)str;
2、string转换为CString:
// 使用C++标准库
std::string str = "Hello World";
CString cstr(str.c_str());
// 使用MFC
std::string str = "Hello World";
CString cstr(str.c_str());
CStringA str(cstr);
CString、string与char数组的转换
1、CString转换为char数组:
CString cstr = _T("Hello World");
char* pStr = cstr.GetBuffer(cstr.GetLength());
2、char数组转换为CString:
char* pStr = "Hello World";
CString cstr(pStr);
3、string转换为char数组:
std::string str = "Hello World";
char* pStr = new char[str.length() + 1];
strcpy(pStr, str.c_str());
4、char数组转换为string:
char* pStr = "Hello World";
std::string str(pStr);
总结
以上就是为你整理的c++编程之cstring、string与char数组怎么进行转换全部内容,希望文章能够帮你解决相关问题,更多请关注本站相关栏目的其它相关文章!