是否有类似getdelim的函数是c ++?

问题描述 投票:0回答:2

c ++中是否有一个功能类似于C中的getdelim功能?我想使用std::ifstream对象处理文件,因此在这里不能使用getdelim

c++ c file-io
2个回答
4
投票

[std::getlinestd::getline的自由功能和char缓冲区的成员都具有带分隔符的重载(BTW std::string是GNU扩展)]


1
投票

如果可以使用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
© www.soinside.com 2019 - 2024. All rights reserved.