简体   繁体   中英

Fast input/output for Competitive Programming

I see people using this snippet extensively for achieving fast i/o in competitive programming

ios_base::sync_with_stdio(false);
cin.tie(NULL);

Though i understand what it does mostly from here:

Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);

I want to know why this statement is not included

cout.tie(NULL);

ie" What difference does this make to the program or does this achieve the same objective as that of

cin.tie(NULL);

Also is it necessary to use NULL or false instead of 0 and 1.

cout is tied to cin , not the other way around.

cout being tied to cin means cout is flushed automatically when reading from cin .

cout.tie(nullptr); would be pointless, as cout.tie() is already nullptr .


NULL is an equivalent of 0 . The C++ type-safe way to express a null pointer is nullptr .

As you can see in this example and in the docs , std::cin and std::cerr are both tied to std::cout by default, but the relationship is one-way. There is no need to call cout.tie(NULL) because the tied stream is already nullptr . The statement would have no effect.

You would not be able to use an argument of 1 ; tie takes a pointer to a std::ostream . 0 / NULL / false all work to untie the streams due to implicit conversions, but to tie two streams together, you would need something like std::cout.tie(&std::cerr) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM