_編程語言_C++_setw()
阿新 • • 發佈:2017-06-19
c++ style space 分享 使用 顯示 整數 ace mes
C++ 中使用setw(int n) 來控制輸出間隔.
例如:
cout<<‘s‘<<setw(8)<<‘a‘<<endl;//s與a之間有7個空格,setw()只對後面緊跟的輸出產生作用.
顯示:s a
setw()默認填充空格,可以修改填充其他
cout<<setfill(‘*‘)<<setw(5)<<‘a‘<<endl;
輸出: ****a
例如:
#include <iostream> using namespace std; #include<iomanip> using std::setw; int main () { int n[ 10 ]; // n 是一個包含 10 個整數的數組 // 初始化數組元素 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // 設置元素 i 為 i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // 輸出數組中每個元素的值for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; }
輸出
_編程語言_C++_setw()