4

I have the following code

cout << setfill('0') << setw(4) << hex << 100 << 100 << std::endl;

The output is:

006464

If I want to let every number with width 4, I have to use

out << setfill('0') << setw(4) << hex << 100 << sew(4) << 100 << std::endl;

But if I want to print every number with hex and setfill('0'), I only need to set setfill('0') and std::hex once.

Does c++ design this on purpose? what is its intention?

zhihuifan
  • 879
  • 1
  • 9
  • 22

1 Answers1

2

Yes it is on purpose. The stream operations are internally peppered with resets of the field width, specified by the standard. I think there's no good answer as to why.

Cheers and hth. - Alf
  • 135,616
  • 15
  • 192
  • 304