[Solved] is setw() and “\t” the same thing? [closed]


They have almost nothing in common.


std::setw(int n) set the width of the next element that goes into the stream. So if you have things like:

std::cout << "Hi," << std::setw(12) << "there!";

This would print:

Hi,      there!
   ^^^^^^ <- 6 empty spaces were made here to fill the width

If you set the width to be longer than the actually object streamed in to it, it will automatically fill them with spaces.

On the other hand, '\t' is a predefined escape sequence. And it will behave similar to when you type a tab in many text editors. Also note that it is actually a character, you could put that in any strings:

std::cout << "\tHi,\tthere!";

This would print:

    Hi, there!
^^^^   ^ <-- both of them are tabs

Note those tabs were made different sizes, you should be able to observe similar behaviors when using tabs in text documents. It will try to fill the current 4 block text with spaces if it was not filled yet.

solved is setw() and “\t” the same thing? [closed]