Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么我要使用其中一个?它们似乎都只影响下一个输出的字段。故意用两种方法做同一件事有什么意义?还是事后才想到的?
std::ios_base::width()是ios_base(好吧,这很明显)的成员,它通常不是用户搞乱的类 - 用户的预期接口是 *stream 类。std::setw()在那个级别工作(并且最终还是会调用std::ios_base::width())。这使编码人员能够编写更清晰的代码(无论如何,我认为):
std::ios_base::width()
ios_base
std::setw()
cout << setw(10) << someint;
代替:
cout.width(10); cout << someint;