c ++中是否有一个功能类似于C中的getdelim
功能?我想使用std::ifstream
对象处理文件,因此在这里不能使用getdelim
。
[std::getline
,std::getline
的自由功能和char缓冲区的成员都具有带分隔符的重载(BTW std::string
是GNU扩展)]
如果可以使用Boost,那么我建议使用getdelim
库。下面的示例使用空格和分号作为分隔符来对流进行标记化:
Tokenizer
输出:
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
#include<algorithm>
int main() {
typedef boost::char_separator<char> Sep;
typedef boost::tokenizer<Sep> Tokenizer;
std::string str("This :is: \n a:: test");
Tokenizer tok(str, Sep(": \n\r\t"));
std::copy(tok.begin(), tok.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
如果要标记输入流的内容,可以轻松完成:
This
is
a
test