boost分割字符串

boost分割字符串
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
using namespace std;

static inline void DeleteElementFrom(const std::string& value, std::vector<std::string>& vec)
{
    std::vector<std::string>::iterator iter;
    for(iter = vec.begin(); iter != vec.end();)
    {
        if((*iter).compare(value) == 0)
            iter = vec.erase(iter);
        else
            ++iter;
    }
}

static inline std::vector<std::string> SplitString(const std::string& str,
        const std::string& delimiter) {
    std::vector<std::string> vec_temp;
    boost::algorithm::split(vec_temp, str,
            boost::algorithm::is_any_of(delimiter));
    DeleteElementFrom("", vec_temp);
    return vec_temp;
}

bool get_bind_device_id(string& bind_deviceid, string& old_device, string& need_deviceid_str, int size = 3) {
    vector<std::string> vec_str;
    vec_str = SplitString(old_device, "||");
    vector<std::string>::iterator iter_t;
    std::string temp = "";
    // 循环查找比较分割后的字符串并进行重复处理
    try {
        for (vector<std::string>::iterator iter = vec_str.begin();
                iter != vec_str.end();) {
            if (*iter == bind_deviceid) {
                temp = *iter;
                iter = vec_str.erase(iter);
            } else {
                iter++;
            }
        }
    } catch (...) {
        return false;
    }
    // 把处理过字符串,放到数组末尾
    if (!temp.empty()) {
        vec_str.push_back(temp);
    } else {
        vec_str.push_back(bind_deviceid);
    }
    //判断数组得大小, 流程会控制它最大是3个
    if (vec_str.size() > size) {
        //删除第一个元素
        vector<string>::iterator it_begin = vec_str.begin();
        vec_str.erase(it_begin);
    }
    //重新组合字符串
    vector<string>::reverse_iterator it = vec_str.rbegin();
    while (it != vec_str.rend()) {
        need_deviceid_str = *it + "||" + need_deviceid_str;
        ++it;
    }
    //字符串截断处理
    need_deviceid_str = need_deviceid_str.substr(0, need_deviceid_str.length()-2);
    return true;
}

int main() {
    /*
    这个主要是用来字符串分割的效果 分割符号|
    并添加原来老的字符进行组合
    */
    std::string need_deviceid_str = "";
    int size = 3;
    std::string bind_deviceid = "a";
    std::string old_device = "c||b";
    get_bind_device_id(bind_deviceid, old_device, need_deviceid_str);
    cout << "need_deviceid_str == " << need_deviceid_str;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值