How does std::setw work with string output?

假如想象 提交于 2019-12-06 10:49:39

I suspect your understanding of std::setw is simply not correct. I think you need something more along the lines of a combination of:

What is happening in your code:

  • Uses std::setw(5) to establish a field width of five characters.
  • Sends "first" to the stream, which is five characters long, so the established field width is completely consumed. No additional filling takes place.
  • Sends "second" to the stream, which is six characters long, so again, the entire field width is consumed (and in-fact breached). Again, no filling takes place

If you're intent is to have something like this (with column numbers above to show positions):

 col: 0123456789012345678901234567890123456789
      first     second    third     fourth

Notice how each word starts on an even multiple of 10 boundary. One way to do that is by using :

  • A output position std::left (so the fill, if any goes on the right to achieve the desired width). This the default for strings, but it never hurts to be sure.
  • A fill character of std::setfill(' '). Again, the default.
  • A field width std::setw(10) Why such a large number? See below

Example

#include <iostream>
#include <iomanip>

int main ()
{
    std::cout << std::left << std::setfill(' ')
              << std::setw(10) << "first"
              << std::setw(10) << "second"
              << std::setw(10) << "third"
              << std::setw(10) << "fourth" << '\n';
    return 0;
}

Output (column numbers added)

0123456789012345678901234567890123456789
first     second    third     fourth

So what happens if we change the output location to std::right ? Well, with the identical program, changing only the first line to :

std::cout << std::right << std::setfill(' ')

we get

0123456789012345678901234567890123456789
     first    second     third    fourth

Finally, one constructive way of seeing where the fill characters are being applied is by simply changing the fill char to something visible (ie. something besides a space). The last two examples output, changing the fill char to std::setfill('*') produces the following output:

First

first*****second****third*****fourth****

Second

*****first****second*****third****fourth    

Notice in both cases, since none of the individual output items breached the std::setw value, the total output line size for each is the same. All that changed was where the fills were applied and the output aligned within the std::setw specification.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!