欢迎使用CSDN-markdown编辑器

来源:互联网 发布:罗杰疑案知乎 编辑:程序博客网 时间:2024/06/15 18:09

C++ split函数

#include "vector"#include <string>#include <iostream>void split(std::string str,std::string pattern, std::vector<std::string> &result){    std::string::size_type pos;    std::string strTemp(str + pattern);    size_t size = strTemp.size();    for(int i = 0; i < size; i++)    {        pos = strTemp.find(pattern, i);        if(pos < size && i < pos)        {            std::string s = strTemp.substr(i,pos-i);            result.push_back(s);            i = (int)(pos+pattern.size()-1);        }    }}typedef std::vector<std::vector<std::string>> VEC_VEC_STR;bool getLineToVector(const char *pszFileName, VEC_VEC_STR &vecLines){    FILE *pFile = NULL;    char szLine[1024];    if((pFile = fopen(pszFileName,"r")) == NULL)    {        std::cout << __FUNCTION__ << "::open file error!" << std::endl;        return false;    }    std::string strLine = "";    while (!feof(pFile))    {        memset(szLine, 0, 1024);        fgets(szLine,1024,pFile);        while (szLine[strlen(szLine) - 1] == '\n'               || szLine[strlen(szLine) - 1] == '\r') {            szLine[strlen(szLine) - 1] = '\0';        }        strLine = szLine;        if (std::string::npos != strLine.find("#")) {            //log            continue;        }        //Dealsub//        std::vector<std::string> vecLine;//        split(strLine, ",", vecLine);//        if (vecLine.size() != 3)//        {//            //log//            continue;//        }//        else {//            vecLines.push_back(vecLine);//        }    }    fclose(pFile);    return true;}
0 0
原创粉丝点击