语法:
头文件:#include
std::left左对齐
std::right右对齐
setw(宽度)
例:
#include#include using namespace std; int main() { cout << std::left << setw(5) << 455 << " " << std::right << setw(5) << 455 << endl; cout << std::left << setw(5) << -123 << " " << std::right << setw(5) << -123 << endl; cout << std::left << setw(5) << 987654 << " " << std::right << setw(5) << 987654< 这里第7行的cout,左对齐5格,输出455,输出空格,右对齐5格,输出455。
左对齐一格
左对齐二格
左对齐三格
左对齐四格
左对齐五格
左对齐五格加空格
左对齐五格加空格加右对齐一格
左对齐五格加空格加右对齐二格
左对齐五格加空格加右对齐三格
左对齐五格加空格加右对齐四格
左对齐五格加空格加右对齐五格
全部打印为:
总结:符号也算一个宽度。并且,如果输出长度超过规定的宽度,则不会受宽度限制



