自定义实现std::string 分割函数

typedef enum EM_ValueType
{
    VT_INT = 0,     //int类型
    VT_DOUBLE,      //double类型
    VT_STRING       //std::string类型
} ValueType;

template<typename T>
void ParseReadLine(const std::string& readLine, const std::string& split, ValueType type, std::vector<T>& vecParseResult)
{
    std::string::size_type pos1, pos2;
    pos2 = readLine.find(split);
    pos1 = 0;

    while (std::string::npos != pos2)
    {
        std::string tempStr = readLine.substr(pos1, pos2 - pos1);

        switch (type)
        {
            case VT_INT:
                vecParseResult.push_back(atoi(tempStr.c_str()));
                break;

            case VT_DOUBLE:
                vecParseResult.push_back(atof(tempStr.c_str()));
                break;

            default:
                vecParseResult.push_back(atoi(tempStr.c_str()));
                break;
        }

        pos1 = pos2 + split.size();
        pos2 = readLine.find(split, pos1);
    }

    if (pos1 != readLine.length())
    {
        std::string tempStr = readLine.substr(pos1);

        switch (type)
        {
            case VT_INT:
                vecParseResult.push_back(atoi(tempStr.c_str()));
                break;

            case VT_DOUBLE:
                vecParseResult.push_back(atof(tempStr.c_str()));
                break;

            default:
                vecParseResult.push_back(atoi(tempStr.c_str()));
                break;
        }
    }
}

注意: 上述模板函数如果要增加 T=std::string 类型时,不能直接在该函数中增加,否则会引起编译错误
错误原因: 因为模板函数体内有多种情况增加元素,此时会使编译器不知道初始化模板为哪种类型而报错!!
解决方法: 重载该函数!!如下所示:

void ParseReadLine(const std::string& readLine, const std::string& split, ValueType type, std::vector<std::string>& vecParseResult)
{
    std::string::size_type pos1, pos2;
    pos2 = readLine.find(split);
    pos1 = 0;

    while (std::string::npos != pos2)
    {
        std::string tempStr = readLine.substr(pos1, pos2 - pos1);

        switch (type)
        {
            case VT_STRING:
                vecParseResult.push_back(tempStr);

            default:
                vecParseResult.push_back(tempStr);
                break;
        }

        pos1 = pos2 + split.size();
        pos2 = readLine.find(split, pos1);
    }

    if (pos1 != readLine.length())
    {
        std::string tempStr = readLine.substr(pos1);

        switch (type)
        {
            case VT_STRING:
                vecParseResult.push_back(tempStr);

            default:
                vecParseResult.push_back(tempStr);
                break;
        }
    }
}

再增加几种函数重载,可以在解析数据的同时,根据传入的处理函数对数据进行处理!!

void ParseReadLine(const std::string& readLine, const std::string& split, std::vector<int>& vecParseResult, std::function<void(int&)> fun)
{
    std::string::size_type pos1, pos2;
    pos2 = readLine.find(split);
    pos1 = 0;

    while (std::string::npos != pos2)
    {
        std::string tempStr = readLine.substr(pos1, pos2 - pos1);
        int value = atoi(tempStr.c_str());
        fun(value);
        vecParseResult.push_back(value);
        pos1 = pos2 + split.size();
        pos2 = readLine.find(split, pos1);
    }

    if (pos1 != readLine.length())
    {
        std::string tempStr = readLine.substr(pos1);
        int value = atoi(tempStr.c_str());
        fun(value);
        vecParseResult.push_back(value);
    }
}

void ParseReadLine(const std::string& readLine, const std::string& split, std::vector<double>& vecParseResult, std::function<void(double&)> fun)
{
    std::string::size_type pos1, pos2;
    pos2 = readLine.find(split);
    pos1 = 0;

    while (std::string::npos != pos2)
    {
        std::string tempStr = readLine.substr(pos1, pos2 - pos1);
        double value = atof(tempStr.c_str());
        fun(value);
        vecParseResult.push_back(value);
        pos1 = pos2 + split.size();
        pos2 = readLine.find(split, pos1);
    }

    if (pos1 != readLine.length())
    {
        std::string tempStr = readLine.substr(pos1);
        double value = atof(tempStr.c_str());
        fun(value);
        vecParseResult.push_back(value);
    }
}

void ParseReadLine(const std::string& readLine, const std::string& split, std::vector<std::string>& vecParseResult, std::function<void(std::string&)> fun)
{
    std::string::size_type pos1, pos2;
    pos2 = readLine.find(split);
    pos1 = 0;

    while (std::string::npos != pos2)
    {
        std::string tempStr = readLine.substr(pos1, pos2 - pos1);
        fun(tempStr);
        vecParseResult.push_back(tempStr);
        pos1 = pos2 + split.size();
        pos2 = readLine.find(split, pos1);
    }

    if (pos1 != readLine.length())
    {
        std::string tempStr = readLine.substr(pos1);
        fun(tempStr);
        vecParseResult.push_back(tempStr);
    }
}
//定义一个局部函数
auto b = [](double & a)
{
    a /= 2;
};

调用范例如下
std::string str = “66.6,35.8,21.6”;
std::vector vec;
ParseReadLine(str, “,”, vec,b);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值