umangahuja11's blog

By umangahuja11, 11 days ago, In English,

Many people use endl and '\n' interchangeably without understanding the key difference between the functioning of the two.

Sometimes using endl in place of '\n' can end up getting you a TLE which otherwise would have been an AC with the use of '\n'.

So let's see what the difference is.

'\n' : It inserts a new line to the output stream.

endl : It inserts a new line and flushes the output stream.

So eventually

cout<<endl;

is equivalent to using

cout<<'\n'<<flush;

Conclusion

Using '\n' outperforms endl in terms of speed (or time) when matter is related to new line only.

Related Problem

The problem which I solved and got TLE issue was Escape from Stones. Go and have a shot at it.

 
 
 
 
  • Vote: I like it  
  • +119
  • Vote: I do not like it  

»
10 days ago, # |
  Vote: I like it +37 Vote: I do not like it

Why down-votes? seems useful to me.

  • »
    »
    10 days ago, # ^ |
      Vote: I like it +25 Vote: I do not like it

    Thanks. I appreciate it. Don't know how blogs work here but I will keep posting stuff I find helpful.

    • »
      »
      »
      10 days ago, # ^ |
        Vote: I like it +18 Vote: I do not like it

      I guess some people down-vote based on colour, or they feel things like these are too basic (even though a lot of people don't know about it). Anyways, keep posting!

»
10 days ago, # |
  Vote: I like it +3 Vote: I do not like it

Heck just put #define endl '\n' at the beginning of the code

  • »
    »
    10 days ago, # ^ |
      Vote: I like it +29 Vote: I do not like it

    Have a nice time fixing IL1 in interactive problems

    • »
      »
      »
      10 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Then comment the line!

      • »
        »
        »
        »
        10 days ago, # ^ |
          Vote: I like it +18 Vote: I do not like it

        Well, you will never remember that untill you get IL) So it will cost you 20 minutes every time you solve an interactive problem. I don't think your teammates will appreciate that kind of a performance))

        • »
          »
          »
          »
          »
          9 days ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Well, you can remember to write cout.flush() when you solve interactive problems.

          • »
            »
            »
            »
            »
            »
            9 days ago, # ^ |
            Rev. 2   Vote: I like it 0 Vote: I do not like it

            This doesn't help. I don't understand why, but sometimes you'll still get IL 1. Check 44326991 for an example (differs from 44326589 only in the line with define)

    • »
      »
      »
      10 days ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Wow that's incredible!!!

      I always define this, but never had any problem with interactive problems, but why??

      • »
        »
        »
        »
        10 days ago, # ^ |
        Rev. 4   Vote: I like it +27 Vote: I do not like it

        If you don't use ios_base::sync_with_stdio(false) cout will be flushed after any output regardless.

        And even if you use previous command, but don't use cin.tie(nullptr) cout will be flushed before any read from cin. And in the most of interactive problems you write something to output and then immediately read the answer from input.

        So for making bufferization fully work (and by this speeding up your I/O) you need to use all three things in your template (this two + #define endl '\n'), not just one. And if you use all three, interactive problems won't work.

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I just put const char en='\n';and use en instead of endl in non-interactive problems.

»
9 days ago, # |
  Vote: I like it +1 Vote: I do not like it

Here is Another problem where you face requirement of '\n' instead of endl. Same submission of mine got TLE with endl and got accepted with '\n'