简体   繁体   English

使用多个字符串分隔符拆分字符串

[英]Split string using multiple string delimiters

Suppose I have string like 假设我有字符串

   Harry potter was written by J. K. Rowling

How to split string using was and by as a delimiter and get result in vector in C++? 如何使用wasby作为分隔符拆分字符串并在C ++中获取vector中的结果?

I know split using multiple char but not using multiple string. 我知道使用多个char但不使用多个字符串进行拆分。

If you use c++11 and clang there is a solution using a regex string tokenizer: 如果你使用c ++ 11和clang,有一个使用正则表达式字符串标记器的解决方案:

#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>

int main()
{
   std::string text = " Harry potter was written by J. K. Rowling.";

   std::regex ws_re("(was)|(by)"); 
   std::copy( std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1),
              std::sregex_token_iterator(),
              std::ostream_iterator<std::string>(std::cout, "\n"));


}

The output is : 输出是:

Harry potter 
 written 
 J. K. Rowling.

Sadly gcc4.8 does not have the regex fully integrated. 可悲的是gcc4.8没有正则表达式完全集成。 But clang does compile and link this correctly. 但clang确实可以正确编译和链接。

Brute force approach, not boost, no c++11, optimizations more than welcome: 蛮力方法,而不是提升,没有c ++ 11,优化超过欢迎:

/** Split the string s by the delimiters, place the result in the 
    outgoing vector result */
void split(const std::string& s, const std::vector<std::string>& delims,
           std::vector<std::string>& result)
{
    // split the string into words
    std::stringstream ss(s);
    std::istream_iterator<std::string> begin(ss);
    std::istream_iterator<std::string> end;
    std::vector<std::string> splits(begin, end);

    // then append the words together, except if they are delimiter
    std::string current;
    for(int i=0; i<splits.size(); i++)
    {
        if(std::find(delims.begin(), delims.end(), splits[i]) != delims.end())
        {
            result.push_back(current);
            current = "";
        }
        else
        {
            current += splits[i] + " " ;
        }
    }

    result.push_back(current.substr(0, current.size() - 1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM