which method is more effective, or faster, well better.
orCode:cout<<"line 1"<<endl<<"line 2";
Code:cout<<"line 1\nline 2";
Printable View
which method is more effective, or faster, well better.
orCode:cout<<"line 1"<<endl<<"line 2";
Code:cout<<"line 1\nline 2";
The second is faster.
Not only is it less function calls, it also has the newline embedded.
The main kicker, though, is the non-use of endl. That actually outputs a newline, then flushes the stream.
Therefore the second makes more efficient use of the buffer.
...But they slightly differ in their effect.
Try putting a getch(); after each one (assuming you use VC++) and testing them separately.
They do, but if one of the effects is unwanted then the other most probably is too. And you shouldn't rely on that.
Anyway:
cout<<"line 1"<<endl<<"line 2"<<endl;
and
cout<<"line 1\nline 2"<<endl;
have exactly the same effect with the second being much better.
Or just call cout.flush(); if you don't want the '\n' to be printed.Code:cout << endl;
// is the same as
cout << '\n';
cout.flush();