C++ 字符串

方法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());
} 

posted on 2010-09-24 13:32  一路前行  阅读(268)  评论(0编辑  收藏  举报