有没有办法告诉 istream 继续前进,直到它到达 \n 而不是正常的空白,并且不使用 getline 并且还在流中保留任何格式选项?
谢谢。
有什么问题std::getline()
?无论如何,如何编写自己的函数:
#include <iostream>
#include <iterator>
template<class In, class Out, class T>
Out copy_until(In first, In last, Out res, const T& val)
{
while( first != last && *first != val ) *res++ = *first++;
return res;
}
// ...
std::string line;
copy_until(std::istreambuf_iterator<char>(std::cin),
std::istreambuf_iterator<char>(),
std::back_inserter(line), '\n');
std::cout << line << std::endl;
该函数将使用换行符,但不会将其放在输出中。