I was solving a problem on LeetCode 121. Best Time to Buy and Sell Stock and after submitting my answer I went and checked the best answer with 0ms run time and had problems understanding it. Can someone explain to me what theses exactly do in beginner terms and how can I use them?
cin.tie(nullptr)->sync_with_stdio(false);
freopen("user.out", "w", stdout);
It said something like the first line was for speeding up by disabling sync and the other was for redirecting output to “user.out” but I can’t seem to comprehend it.
Thanks in advance!
Shubham Mittal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
This is basically cheating, by optimize IO operation done by test application. Most of this changes are preventing flushing buffers of standard output, so they are used in more optimal way.
-
cin.tie(nullptr);
Prevents flushing ofstd::cout
whenever input data are read formstd::cin
. See std::istream::tie documentation. -
->sync_with_stdio(false);
badly written invocation of static function: std::ios::sync_with_stdio to prevent synchronization between C and C++ streams. -
freopen("user.out", "w", stdout);
redirecting C streamstdout
directly to file (not sure if this helps at all).