C ++升级拆分而不使用is_any_of()

时间:2017-04-13 05:23:50

标签: c++ boost

以下函数将输出{" a"," b"," c"}作为字符串输入的矢量" a = b + C"当分隔符为" + ="

void Split( const std::string &str, std::vector<std::string> & tokens , std::string delim)
{
     boost::split(tokens, str, boost::is_any_of(delim));
}

我需要输出{&#34; a = b + c&#34;} 请建议修改或任何适用的功能

1 个答案:

答案 0 :(得分:0)

这是您正在寻找的代码,常规split只需要字符,因此您必须使用split_regex代替:

#include <string>
#include <iostream>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <vector>

int main() {
    std::string str = "a=b+c" ;
    std::vector<std::string> result;
    boost::algorithm::split_regex(result, str, boost::regex("\\+="));
}

请注意,您必须使用boost_regex将其与-lboost_regex相关联,否则会向您发送错误。

同样在字符串"\\+="中,有两个反斜杠的原因是正则表达式使用'+'作为特殊字符,因此您必须使用'\'字符来逃避该用法,但有一个'\'将其转换为特殊字符'\+',因此您必须像第一个'\'一样转义:'\\+='