Programmatically Ignore Cout

后端 未结 2 584
孤街浪徒
孤街浪徒 2021-01-04 17:57

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

2条回答
  •  滥情空心
    2021-01-04 18:36

    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";
    

    Edit:

    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";
    

提交回复
热议问题