c++ - 在下一个之前检测行尾

标签 c++ stream

我不知道如何在以下代码中的下一行之前“缓冲”一行输入(并对该行执行一些操作,例如在末尾插入换行符):

#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/

相关文章:

c++ - 我必须手动解构所有对象吗

c++ - 视差映射 - GLSL- OpenGL

c++ - 默认构造函数中未识别的标识符

node.js - 将多个图像流通过管道传输到 ImageMagick/GraphicsMagick 子进程

c++ - C++中的高速缓冲

Python,将Popen的流重定向到python函数

c++ - 如何使用 Armadillo 计算特征对?

c++ - 为什么 QIterator 对象对于放置在两个不同位置的同一个文件夹表现不同?

java - 使用 zip4j 损坏 NiFi ExecuteScript 输出

python - 快速数据从文件移动到一些 StringIO