Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: All
Jul 17, 2000

Minimize the Use of the endl Manipulator

Many programmers append the endl manipulator after each cout expression, as in:

 
cout << "user login" << endl;
cout << "enter your name: " << endl;
cin >> name;  

In the first line, the endl manipulator is added to force a line break. However, it's cheaper in terms of performance to use the '\n' character instead of endl because endl also flushes the stream rather than just adding a line break. Flushing a stream can be a costly operation. The second cout expression too has a redundant endl manipulator because the display must appear on the screen before the cin expression is executed, as cin and cout are tied.

The bottom line: avoid using the endl manipulator if you can because it slows down your program.

Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap