方法1:用c/c++自身的字符串数组
#include <string.h>
void test()
{
// 用法 1 - 1
char szInfo[100] = {0};
strcpy(szInfo, "hello, world\r\n");
printf(szInfo);
// 用法 1- 2
char *pInfo = "hello, guys\r\n";
printf(pInfo);
// 用法 1 - 3
char *pThird = new char[100];
strcpy(pThird, "hello, third method\r\n");
printf(pThird);
}
方法2: 用 STL 封装的字符串 ,好处是重载了许多运算符,使得字符串的连接等操作很方便
#include <string>
using namespace std;
void test2()
{
string str;
str = "hello, ";
str += "world\r\n";
printf(str.c_str());
}