umangahuja1's blog

By umangahuja1, 6 years 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.

Full text and comments »

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