以编程方式忽略Cout [英] Programmatically Ignore Cout

查看:88
本文介绍了以编程方式忽略Cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否有技巧来切换所有 cout<< 函数以不输出可见输出?我试图将我和其他人编写的一些代码汇总在一起,以组成一个演示。我宁愿不将输出重定向到文件,而是想要一种在Windows和Linux之间具有某种程度兼容性的解决方案。

Does anybody know if there is a trick to toggle all the cout << functions to not print out visible output? I am trying to hack together some code written by me and some other people to put together a demo. I would rather not redirect the output to a file and would like a solution that had some measure of compatibility between Windows and Linux.

在我的场景中,我有很多行带有某些 #defines 控制某些方法何时生成调试输出的代码。我想称呼类似的东西:

In my scenario I have many many lines of code with with various #defines controlling when certain methods produce debug output. I want to call something like:

cout.off();
driverForAffectA();
driverForAffectB();
cout.on();
printSpecializedDebug();
exit(0);


推荐答案

您可以更改cout的流缓冲区。

You can change cout's stream buffer.

streambuf *old = cout.rdbuf();
cout.rdbuf(0);
cout << "Hidden text!\n";
cout.rdbuf(old);
cout << "Visible text!\n";

编辑:

感谢John Flatness '评论,您可以缩短代码:

Thanks to John Flatness' comment you can shorten the code a bit:

streambuf *old = cout.rdbuf(0);
cout << "Hidden text!\n";
cout.rdbuf(old);
cout << "Visible text!\n";

这篇关于以编程方式忽略Cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆