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

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

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
  • Проголосовать: не нравится

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

Why down-votes? seems useful to me.

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

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

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +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'