0

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

i.e" 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.

Evadore
  • 107
  • 1
  • 9
  • I would venture that just using C-style I/O would be simpler than trying to get C++ iostreams to be performant... – Cameron Sep 21 '20 at 15:57

2 Answers2

6

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.

rustyx
  • 80,671
  • 25
  • 200
  • 267
5

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).

0x5453
  • 12,753
  • 1
  • 32
  • 61