我想了解一下manipulators...is有没有具体的订单呢?
例如,std::setw
是在std::setfill
之后还是之前,它们应该在单独的行中吗?
发布于 2018-03-28 09:14:51
没有特定的顺序,只需确保包含<iomanip>
库即可。
关于setw/setfil问题的示例:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << setfill('*');
cout << 123;
}
发布于 2018-03-28 20:09:58
没有具体的顺序。但请注意这一点,例如,如果您想使用std::left和std::right,或者在一行中编写所有内容,那么事情可能会变得有点棘手。
例如,这不会打印预期的输出(仅打印:7
):
std::cout << std::setw(10) << std::left << 7 << std::setfill('x') << std::endl;
因为您需要先设置属性,然后再打印您想要的任何内容。因此,下面的三行代码都可以工作,不管它们的位置发生了什么变化(打印:xxxxxxxxx7
):
std::cout << std::setw(10) << std::setfill('x') << std::right << 7 << std::endl;
std::cout << std::right << std::setw(10) << std::setfill('x') << 7 << std::endl;
std::cout << std::setfill('x') << std::right << std::setw(10) << 7 << std::endl;
下面的代码只是为了澄清一些事情。
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(15) << std::setfill('-') << "PRODUCT" << std::setw(15) << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::setfill('-') << "Brush" << std::setw(15) << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::setfill('-') << "Paste" << std::setw(15) << std::setfill('-') << 8 << std::endl << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "PRODUCT" << std::setw(15) << std::left << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "Brush" << std::setw(15) << std::left << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "Paste" << std::setw(15) << std::left << std::setfill('-') << 8 << std::endl << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "PRODUCT" << std::setw(15) << std::right << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "Brush" << std::setw(15) << std::right << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "Paste" << std::setw(15) << std::right << std::setfill('-') << 8 << std::endl << std::endl;
return 0;
}
https://stackoverflow.com/questions/49524551
复制相似问题