-4

What is the difference between

cin.tie(0);
ios:sync_with_stdio(0);

and

cin.tie(0) -> sync_with_stdio(0);

I do this so that both cin and cout become faster and I use the cin.tie(0) to reduce the runtime when I am interleaving cin and cout. I just want to know what the difference is between the two because I can't find an answer anywhere. Thanks in advance.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    Neither of them is C and C++. Why have you tagged it with these languages? – 273K Jun 25 '22 at 23:50
  • what are you talking about? This is c/c++??? https://usaco.guide/general/fast-io?lang=cpp – gnarayan Jun 25 '22 at 23:51
  • I can't compile either of these snippets, there are missing a number of code lines to make these valid. – πάντα ῥεῖ Jun 25 '22 at 23:56
  • Relevant background reading: [Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);](https://stackoverflow.com/q/31162367/11082165) – Brian61354270 Jun 25 '22 at 23:59
  • 1
    Does this answer your question? [std::ios\_base::sync\_with\_stdio(false), advantages, disadvantages?](https://stackoverflow.com/questions/70302865/stdios-basesync-with-stdiofalse-advantages-disadvantages). As explained there, `sync_with_stdio` is a static member function, so it doesn't matter which instance of `ios_base` you call it on (or whether you call it on an instance at all). Whoever wrote the second snippet either doesn't know what `sync_with_stdio` is, or they intentionally obfuscated their code for a "coolness" factor – Brian61354270 Jun 26 '22 at 00:01
  • @Brian More likely they just want to save a few key strokes, since the goal here is to write the program as quickly as possible. Of course using such code as reference is a terrible way to learn the language. – user17732522 Jun 26 '22 at 00:27
  • @gnarayan The code is definitively not C at all. And in C++ `:` doesn't make sense there. You probably mean `::`. – user17732522 Jun 26 '22 at 00:30
  • 2
    It is not a good idea to learn the language from competitive programming code. Learn the language properly first, e.g. from one of the [recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and then lookup references to the functions you are using, e.g. on https://cppreference.com. Then it would be clear to you what happens here. – user17732522 Jun 26 '22 at 00:34

1 Answers1

1

Assuming you mean :: instead of : and that you are using using namespace std; (which is generally considered bad style because it can lead to problems, see Why is "using namespace std;" considered bad practice?), then there is no difference between the two snippets.

As you can see in a reference for the function std::basic_ios::tie, the overload you are using returns a pointer to the previously tied stream as std::basic_ostream*.

In the reference for the function std::ios_base::sync_with_stdio you can see that it is a static member function of std::ios_base. So we can call it on any instance of std::ios_base. std::basic_ostream inherits from std::ios_base as also shown in the reference, so we can also call it on the std::basic_ostream* pointer returned from cin.tie(0).

-> is just the indirect member access as usual, used here to perform the member function call on the pointer returned by cin.tie(0).

user17732522
  • 53,019
  • 2
  • 56
  • 105