宽度,对齐方式的设置
生活随笔
收集整理的這篇文章主要介紹了
宽度,对齐方式的设置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、輸出寬度
1.使用width函數(shù)控制
1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 double values[] = { 1.23,35.36,653.7,4358.24 }; 6 for (int i = 0; i < 4; i++) { 7 cout.width(10); 8 cout << values[i] << endl; 9 } 10 return 0; 11 }默認為右對齊。運行結果如下:
?
2.使用set操作符控制,有頭文件#include<iomanip>
?
1 #include<iostream> 2 #include<iomanip> 3 #include<string> 4 using namespace std; 5 6 int main() { 7 double values[] = { 1.23,35.36,653.7,4358.24 }; 8 string names[] = { "Zoot","Jimmy","Al","Stan" }; 9 for (int i = 0; i < 4; i++) { 10 cout << setw(6) << names[i] << setw(10) << values[i] << endl; 11 } 12 return 0; 13 }?
運行結果如下:
?
二、輸出寬度
?通過使用帶參的setiosflags操作符來設置左對齊,ios_base::left是ios_base的靜態(tài)常量,必須使用ios_base::前綴。用resetiosflags(ios_base::left)來關閉左對齊標志。
1 #include<iostream> 2 #include<iomanip> 3 #include<string> 4 using namespace std; 5 6 int main() { 7 double values[] = { 1.23,35.36,653.7,4358.24 }; 8 string names[] = { "Zoot","Jimmy","Al","Stan" }; 9 for (int i = 0; i < 4; i++) { 10 cout << setiosflags(ios_base::left) << setw(6) << names[i] << resetiosflags(ios_base::left) << setw(10) << values[i] << endl; 11 } 12 return 0; 13 }運行結果如下:
?
轉載于:https://www.cnblogs.com/Gzu_zb/p/9373954.html
總結
以上是生活随笔為你收集整理的宽度,对齐方式的设置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql查看在线用户
- 下一篇: 2018牛客暑假多校三 E(KMP运用)