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).

4 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.

3

If you want to add the feature yourself, it would look like this:

#include <iostream> namespace std { template <typename _CharT, typename _Traits> inline basic_ostream<_CharT, _Traits> & tab(basic_ostream<_CharT, _Traits> &__os) { return __os.put(__os.widen('\t')); } } int main() { std::cout << "hello" << std::endl; std::cout << std::tab << "world" << std::endl; } 

I don't recommend doing this, but I wanted to add a solution for completeness.

Actually, it is not needed.

Because endl first does the same job of inserting a newline as \n, and then also flushes the buffer.

Inserting \t on a stream does not require to flush it after .

No.

There are only std::ends (insert null character) and std::flush (flush the stream) output manipulators besides std::endl in ostream include file.

You can find others in ios and iomanip include files. Full list is here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy