*(00)*

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
/*
* 拆分字符串
* 参数:
*     strData        字符串
*     split            分隔符
* 返回:
*     返回动态数组std::vector<std::string> ,记得要delete 内存
*/
std::vector<std::string>* GetStringArray(char* strData,char* split)
{
    //定义一个动态数组
    std::vector <std::string> *arr = new std::vector <std::string>();     
    int strLength = strlen(strData);
    //分解成字符串数组
    char *strDataEx = new char[strLength+1];//创建相应长度字符串
    int num = 0 ;//记录指针个数(字符串个数)
    while(*(strData+num) != '\0'){*(strDataEx+num) = *(strData+num); num++;}//将字符串常量转换为变量
    *(strDataEx+num) = '\0';//变量添加结束标识
    //const char *split = ","; 
    char *p; 
    p = strtok(strDataEx,split); 
    while(p!=NULL) {
        //printf ("%s\n",p); 
        arr->push_back(p);//插入动态数组(拷贝数据)
        p = strtok(NULL,split);
    }
    delete strDataEx;//释放指针
    //返回动态数组
    return arr;
}

 

posted on 2013-10-15 09:53  *(00)*  阅读(272)  评论(0编辑  收藏  举报