Using a stream manipulator (endl) or a newline escape character (n)?

I don’t have a specific context in which I’m asking the question, but while I was reading a beginner book on C++ I noticed the use of both an endl stream manipulator and a newline escape character when dealing with a stream object.

The exmaple is as follows:

cout << "Hello World" << endl;
cout << "Hello Worldn";

My questions are:

  1. Is it more appropriate to use the stream manipulator (endl) in a certain
    situation and an escape character in a different one?
  2. Are there drawbacks efficiency wise to using one of the two?
  3. Are they completely interchangeable?
  4. I read that an escape sequence is stored in memory as a single character. Does that mean it is more appropriate to use endl if you’re going for low memory consumption?
  5. Does the stream manipulator endl use up memory in any way, if so is it more than the escape sequence?

Thanks, StackExchange
Apologies if I posted this in the wrong section, I thought it counted as data structures.

1

o << std::endl is equivalent to the following code:

o.put(o.widen('n'));
o.flush();

In other words, you should only use std::endl when you need to flush the stream. For example:

  • You’re about to perform a time-consuming operating and want to make sure you display a message before doing so.
  • Another process is waiting on your output.
  • If more than one thread or process is running, you may need to flush output so that the output from each thread or process is displayed correctly. (Otherwise, you may get seemingly random blocks of output interleaved at awkward intervals.)

If you don’t need to flush the stream, then use n instead of std::endl. The extra calls to flush can hurt performance (sometimes significantly).

For more details, see cppreference.com.

Regarding memory usage: Worrying about n versus std::endl is almost certainly an unnecessary micro-optimization, but in general, I’d expect n to take less memory. n is just one more byte at the end of a string literal, while writing std::endl is translated by the compiler into (probably inlined) function calls to put and flush.

Platform-specific differences in line endings (Windows rn versus Linux and OS X n) are handled at a lower level than std::endl and n:

  • If a stream is opened in text mode, then if you write n, it automatically translates it to the appropriate platform-specific line ending. If you read a platform-specific line ending, the stream automatically translates it to n.
  • If a stream is opened in binary mode, then they pass any line endings through verbatim, and it’s up to you.
  • For std::cout and std::cin in particular, they’re treated as if they’re text mode.

1) For portability, use endl. Windows newlines are rn, Linux n and Mac r. Edit: As per comments, line system specific endings are handled at a lower level.

2) endl flushes the stream, "n" does not.

3) Depends on portability.

As to memory usage, you can minimize it by flushing to other storage as often as possible with endl. However, it’ll decrease performance.

Edit: Clear up some mistakes.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật