- const char* 类型转string类型
在xstring类库重载的运算符"="中,允许直接将const (基础数据类型)*直接转为string类型;
#include <stdio.h>
#include <string>
using namespace std;
void main()
{
const char* cTest = "abc中文1234";
string strTest = cTest;
printf("%s\n", strTest);
printf("%s\n", strTest.c_str());
system("pause");
}
控制台输出结果:
L?
abc中文1234
请按任意键继续. . .
- string 类型转const char*类型
*拓展类库<xstring.h>中的**c_str()**方法可直接将string变量转为const char类型返回。举例如下:
#include <stdio.h>
#include <string>
using namespace std;
void main()
{
string strTest = "abc中文1234";
printf("%s\n", strTest);
const char* cTest = strTest.c_str();
printf("%s\n", cTest);
}
控制台输出结果:
x?
abc中文1234
请按任意键继续. . .