[译] C++ 如何切分字符串

声明:

水平有限,如有翻译不当,欢迎探讨、批评与指正。


帖子内容:

C++ 切分字符串的最优雅的方式是什么?我们假定字符串中每个单词分隔符是空格。 (备注:我对C的字符串函数或者那种字符处理/存取方式不是很感兴趣。因此,请优先选择优雅而不是效率)


我现在能想到的最好的方式是:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string s("Somewhere down the road");
    istringstream iss(s);

    do
    {
        string sub;
        iss >> sub;
        cout << "Substring: " << sub << endl;
    } while (iss);

    return 0;
}

另一种方式去萃取输入字符串的分隔符,使用标准库函数很容易实现。下面是使用 STL 的设计方案(健壮并且优雅):

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

int main() {
    using namespace std;
    string sentence = "Something in the way she moves...";
    istringstream iss(sentence);
    copy(istream_iterator<string>(iss),
             istream_iterator<string>(),
             ostream_iterator<string>(cout, "\n"));
}

如果想要把分割后的字符串放到一个容器中,而不是控制台的话,使用相同的泛型算法即可。

vector tokens;
    copy(istream_iterator(iss),
         istream_iterator(),
         back_inserter >(tokens));[/c]

评:这种方案只能分割空格,没有伸缩性。


使用 boost 库:

std::vector strs;
boost::split(strs, "string to split", boost::is_any_of("\t "));

我打赌这个要比使用 stringstream 快的多。并且这是一个泛型模板方法,它可以用来分割任意类型的字符串(wchar,etc. or UTF-8),使用任意的分隔符。 具体请看文档

评:不是每个人都用 boost 的。


把 delim 作为作为分隔符,第一个函数把结果放到一个已经存在构造好了的 vector 中,第二个函数返回一个新的 vector。

std::vector &split(const std::string &s, char delim, std::vector &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

std::vector split(const std::string &s, char delim) {
    std::vector elems;
    return split(s, delim, elems);

}

注意:这种方法不能跳过空的符号,比如下面有四个数据项,其中的一个是空的。

std::vector x = split("one:two::three", ':');

template < class ContainerT >
void tokenize(const std::string& str, ContainerT& tokens,
              const std::string& delimiters = " ", const bool trimEmpty = false)
{
   std::string::size_type pos, lastPos = 0;
   while(true)
   {
      pos = str.find_first_of(delimiters, lastPos);
      if(pos == std::string::npos)
      {
         pos = str.length();

         if(pos != lastPos || !trimEmpty)
            tokens.push_back(ContainerT::value_type(str.data()+lastPos,
                  (ContainerT::value_type::size_type)pos-lastPos ));

         break;
      }
      else
      {
         if(pos != lastPos || !trimEmpty)
            tokens.push_back(ContainerT::value_type(str.data()+lastPos,
                  (ContainerT::value_type::size_type)pos-lastPos ));
      }

      lastPos = pos + 1;
   }
};

#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string str("Split me by whitespaces");
    string buf; // Have a buffer string
    stringstream ss(str); // Insert the string into a stream

    vector<string> tokens; // Create vector to hold our words

    while (ss >> buf)
        tokens.push_back(buf);

}

笔者注:

First created: 2012-04-12 01:33:44
Last updated: 2022-12-11 Sun 12:49
Power by Emacs 27.1 (Org mode 9.4.4)