Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tab equivalent of std::endl within the standard library?

Tags:

c++

std

Using C++, is there an equivalent standard library constant for '\t' like there is for a newline?

Ideally:

std::stringstream ss; ss << std::tab << "text"; 

If not, why is this the case?

(I'm aware I can just insert a '\t' but I'd like to sate my curiosity).

like image 704
matthewrdev Avatar asked Feb 27 '14 01:02

matthewrdev


People also ask

Where is std :: endl defined?

The std::endl manipulator is defined in <ostream> header. I attempted to clarify the question, I think there is a good question here and it has attracted an excellent answer from Yakk.

What is std :: endl in C++?

std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os ); Inserts a newline character into the output sequence os and flushes it as if by calling os.

Is Endl same as N?

Both endl and \n serve the same purpose in C++ – they insert a new line. However, the key difference between them is that endl causes a flushing of the output buffer every time it is called, whereas \n does not.


1 Answers

No. std::endl isn't a newline constant. It's a manipulator which, in addition to inserting a newline, also flushes the stream.

If you just want to add a newline, you're supposed to just insert a '\n'. And if you just want to add a tab, you just insert a '\t'. There's no std::tab or anything because inserting a tab plus flushing the stream is not exactly a common operation.

like image 136
Brian Bi Avatar answered Sep 20 '22 18:09

Brian Bi