How to disable cout output in the runtime?

前端 未结 5 1118
臣服心动
臣服心动 2020-12-14 08:52

I often use cout for debugging purpose in many different places in my code, and then I get frustrated and comment all of them manually.

Is there a way

5条回答
  •  盖世英雄少女心
    2020-12-14 09:05

    To supress output, you can disconnect the underlying buffer from cout.

    #include 
    
    using namespace std;
    
    int main(){
    
        // get underlying buffer
        streambuf* orig_buf = cout.rdbuf();
    
        // set null
        cout.rdbuf(NULL);
    
        cout << "this will not be displayed." << endl;
    
        // restore buffer
        cout.rdbuf(orig_buf);
    
        cout << "this will be dispalyed." << endl;
    
        return 0;
    }
    

提交回复
热议问题