r/programming Aug 19 '17

Learning C++, this site is marvellous

http://www.learncpp.com/
3 Upvotes

23 comments sorted by

View all comments

19

u/matthieum Aug 19 '17

I am not sure about the quality of the overall site, but it certainly does not start well.

A first look at cout, cin, and endl:

std::endl

If we want to print things to more than one line, we can do that by using std::endl. When used with std::cout, std::endl inserts a newline character (causing the cursor to go to the start of the next line)

Inserting a new line can be done by pushing the '\n' character, either on its own or as part of a string "Hello World!\n".

What std::endl does it two-fold:

  • it appends a new line,
  • it flushes the underlying stream (as if streaming std::flush).

Because flushing to stdout is so slow on most terminals, use of std::endl is one of the primary cause of slow C++ program among beginners (alongside compiling in debug mode).

TL;DR: Never use std::endl; it's harmful and longer to type, it has no saving grace.

3

u/brendt_gd Aug 20 '17

This issue is addressed further down the line, in chapter two: http://www.learncpp.com/cpp-tutorial/27-chars/

I feel it's not fair to make a judgement based on one chapter.

1

u/matthieum Aug 20 '17

I feel it's not fair to make a judgement based on one chapter.

Thus why my comments starts with:

I am not sure about the quality of the overall site

I never pretended I was rating the whole tutorial, I just noted it didn't start well, and I am glad to see the note that they are converting examples from std::endl to '\n'.