我不知道如何在以下代码中的下一行之前“缓冲”一行输入(并对该行执行一些操作,例如在末尾插入换行符):
#include <iostream>
class Test {
public:
template<class T>
Test& operator<< (T&& anything) {
std::cout << anything;
return *this;
}
};
int main() {
Test myobj;
myobj << "hello" << "this is cool";
// How to insert a newline here?
myobj << "but" << "this is NOT cool";
}
我希望能够检测到何时行
myobj << "hello" << "this is cool";
在执行下一个之前已经完成。
最佳答案
`"\n"`
如下所示为您做:
int main() {
Test myobj;
myobj<< "hello" << "this is cool<<"\n"";
// How to insert a newline here?
myobj << "but" << "this is NOT cool";
}
否则你使用std::endl
如下
myobj << "hello" << "this is cool<<std::endl;
关于c++ - 在下一个之前检测行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660753/