替换cout“字符串”在cpp中具有cout<<“string”<< endl [英] Replace cout<<"string" with cout<<"string"<<endl in cpp

查看:248
本文介绍了替换cout“字符串”在cpp中具有cout<<“string”<< endl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要替换出现在程序中的每个 cout ,但 endl 。我试图使用宏,但无法弄清楚如何做到这一点。请帮助!

I want to replace each cout occurring in a program with same but concatenated with endl. I'm trying to use macros for this but unable to figure that out how to do that. Please help!

有一种方法可以在程序中写完整行,只需concat < endl 吗?
注意,如果 endl 已经由程序员编写,则不会连接 endl

Is there a way to get complete line written in program and just concat << endl with it? Note if endl is already written by programmer, no endl will be concatenated.

如果可能有其他更好的方法,请建议。

If any other better method possible, please suggest.

推荐答案

。但我绝不会容忍它。

Unfortunately, this is possible. But in no sense can I condone it.

#include <iostream>

namespace std
{
class not_actually_cout{};

template<typename T>
not_actually_cout& operator<< (not_actually_cout& stream, const T & v)
{
    std::cout << v << std::endl;
    return stream;
}

not_actually_cout not_actually_cout_instance;
}

#define cout not_actually_cout_instance

int main(void)
{
    cout << "why god why";
    cout << "please no";
    return 0;
}

输出:

why god why
please no

这篇关于替换cout“字符串”在cpp中具有cout&lt;&lt;“string”&lt;&lt; endl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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