C++使用多个分隔符分解字符串

whi*_*ets 2 c++

可能重复:
在C++中通过多个分隔符将字符串拆分为单词

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

我的问题是:如何仅使用STL使用多个分隔符拆分一行?我一直试图在这一天的大部分时间都不知所措.

任何建议将非常感谢.

nne*_*neo 9

使用 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++;
}
Run Code Online (Sandbox Code Playgroud)