欢迎使用CSDN-markdown编辑器

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值