在下一个行之前检测行尾
Detect end of line before next one
我不知道如何在以下代码的下一条输入之前"缓冲"一行输入(并在该行上执行一些操作,例如在末尾插入换行符):
#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;
您可以在其析构函数中检测到它。创建一个返回临时的log()
帮助程序函数。在完整表达式结束时,其析构函数将运行:
class Test {
public:
template<class T>
Test& operator<< (T&& anything) {...}
~Test() { std::cout << std::endl; }
};
Test log() { return Test(); }
// ...
log() << "hello" << "this is cool"; // destructor inserts a newline here
log() << "but" << "this is NOT cool"; // here too
相关文章:
- 重新定位图像时如何前进到下一个内存块
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- CIN 仅在输入非数字值时跳过下一个 CIN
- 查找最小的下一个更大的元素
- 转到基于范围的 for 循环中的下一个迭代器
- 如何检查流中文件的下一个单词是否为 alpha?
- 如何打印下一个字母直到 Z 并继续到 A?
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- 使用一个内存集数组和单个堆栈在 O(n) 中查找数组的下一个更大元素
- 有没有一种标准方法可以在C++中获取第 n 个"下一个"浮点值
- 下一个云桌面客户端构建过程
- C++自定义流操纵器,用于更改流上的下一个字符串
- Qt5 检测另一个小部件下的小部件
- 有人能向我解释一下我的代码的哪一部分导致***检测到glibc***吗/a2:free():无效的下一个大小(快速)
- glibc 检测到 *** ./burcu.exe: free(): 无效的下一个大小 (快速): 0x00000000
- **检测到glibc***/main:free():无效的下一个大小(fast):
- ***检测到glibc***..:free():无效的下一个大小(正常):..***fclose();之后;
- 错误glibc检测到free():无效的下一个大小(fast)
- Linux中的c++运行时错误::glibc检测到free():无效的下一个大小(快速):堆栈具有void ptr元素