C++使用多个定界符分解字符串

标签 c++

<分区>

Possible Duplicate:
Split a string into words by multiple delimiters in C++

我目前正在尝试读取一个文件,其中每一行都有制表符和空格的变体,用于分隔需要插入到二叉树中的关键属性。

我的问题是:如何仅使用 STL 使用多个定界符拆分一行?在一天的大部分时间里,我一直在努力解决这个问题,但无济于事。

如有任何建议,我们将不胜感激。

最佳答案

使用std::string::find_first_of

vector<string> bits;
size_t pos = 0;
size_t newpos;
while(pos != string::npos) {
    newpos = str.find_first_of(" \t", pos);
    bits.push_back(str.substr(pos, newpos-pos));
    if(pos != string::npos)
        pos++;
}

关于C++使用多个定界符分解字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079763/

相关文章:

c++ - CMake 在 C++ 项目中包含目录和环境变量

c++ - 为什么 boost::assign::list_of 不适用于 pair<string, vector<string>>?

c++ - 为 Visual Studio 2010 设置 OpenCV-2.3

c++ - 传递字符串 'by value' 本地值的变化反射(reflect)在原始值中

c++ - 初始化数组的更好方法

c++ - 以下内联汇编有什么问题?

c++ - C++字符串程序引起的运行时错误

c++ - 如何更改 ListView 的边框颜色

c++ - Linux C++ 链接器/usr/bin/ld

c++ - gcc 4.8.2 在此默认的默认构造函数中调用复制构造函数是否正确?