Блог пользователя umangahuja1

Автор umangahuja1, 5 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +119
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +37 Проголосовать: не нравится

Why down-votes? seems useful to me.

»
5 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится +29 Проголосовать: не нравится

    Have a nice time fixing IL1 in interactive problems

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Then comment the line!

      • »
        »
        »
        »
        5 лет назад, # ^ |
          Проголосовать: нравится +18 Проголосовать: не нравится

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

        • »
          »
          »
          »
          »
          5 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

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

          • »
            »
            »
            »
            »
            »
            5 лет назад, # ^ |
            Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

            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)

            • »
              »
              »
              »
              »
              »
              »
              4 года назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              I suppose, it's because you has used the speedup define. It disables sync with stdio.

    • »
      »
      »
      5 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      Wow that's incredible!!!

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

      • »
        »
        »
        »
        5 лет назад, # ^ |
        Rev. 4   Проголосовать: нравится +27 Проголосовать: не нравится

        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.

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

»
5 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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'