目錄
簡單了解cout是什么?
什么是字節流
默認格式控制
修改計數系統
調整字符寬度
填充字符
設置浮點數顯示精度
打印末尾的0和小數點
其他格式控制符
? ? ? ? right--->設置為右對齊,永久生效
? ? ? ? left--->設置為左對齊,永久生效
????????fixed--->固定以小數顯示浮點數,永久生效
????????scientific--->固定以科學計數法顯示浮點數,永久生效
簡單了解cout是什么?
? ? ? ? cout是ostream類的一個對象,叫做標準輸出流對象;cin是istream類的一個對象,叫做標準輸入流對象;iostream類繼承了ostream和istream;當程序包含<iostream>時,自動在命名空間std中創建對象cout和cin。
什么是字節流
? ? ? ? 字節流大概就是一個個字節組成的隊列。
????????c++的輸入輸出是面向字節流的。輸入時,程序從輸入流中取數據;輸出時,程序將數據插入到輸出流中。可以理解為c++輸入輸出的單位是單個字節。
int a = 123;
cout << a;
? ? ? ? 此處 a 是 int 類型,占4個字節,底層是32個二進制位。如果一個字節一個字節的取數據,不可能取到1 2 3這三個數,然后插入輸出流進而顯示到顯示器上。ostream類將數據的底層二進制轉化為由一個個字符字節組成的輸出流。?理解這一點很重要。
默認格式控制
- 對于char值,如果它代表的是可打印字符,則將被作為一個字符顯示在寬度為一個字符的字段中。?
- 對于數值整型,將以十進制方式顯示在一個剛好容納該數字及負號(如果有的話)的字段中。
- ?字符串被顯示在寬度等于該字符串長度的字段中。
- 浮點類型被顯示為6位,末尾的0不顯示(注意,顯示的數字位數與數字被存儲時精度沒有任何關系)。數字以定點表示法顯示還是以科學計數法表示,取決于它的值。當指數大于等于6或小于等于-5時,將使用科學計數法表示。另外,字段寬度恰好容納數字和負號(如果有的話)。
#include <iostream>int main()
{using std::cout;cout << "12345678901234567890\n";char ch = 'K';int t = 273;cout << ch << ":\n";cout << t << ":\n";cout << -t <<":\n";double f1 = 1.200;cout << f1 << ":\n";cout << (f1 + 1.0 / 9.0) << ":\n";double f2 = 1.67E2;cout << f2 << ":\n";f2 += 1.0 / 9.0;cout << f2 << ":\n";cout << (f2 * 1.0e4) << ":\n";double f3 = 2.3e-4;cout << f3 << ":\n";cout << f3 / 10 << ":\n";// std::cin.get();return 0;
}
修改計數系統
#include <iostream>
int main()
{using namespace std;cout << "Enter an integer: ";int n;cin >> n;cout << "n n*n\n";cout << n << " " << n * n << " (decimal)\n";
// set to hex modecout << hex; //以16進制輸出cout << n << " ";cout << n * n << " (hexadecimal)\n";// set to octal modecout << oct << n << " " << n * n << " (octal)\n";//以八進制輸出// alternative way to call a manipulatordec(cout);//以十進制輸出cout << n << " " << n * n << " (decimal)\n";// cin.get();// cin.get();return 0;
}
? ? ? ? 代碼說明:
- ostream重載了<<運算符,cout<<hex等價于hex(cout)
- hex(cout)場景下,hex看做是一個函數
- cout << hex場景下,hex看做一個輸出控制符;這一點與flush一個道理(不了解flush的可以看如何刷新緩沖區(c++、c、linux)-CSDN博客)
- 后續的oct、dec同樣的道理
- 一旦使用計數控制符(hex、oct、hec)除非下次修改,否則一直處在這個狀態
調整字符寬度
? ? ? ? int width(int i):將字符寬度設置為i個空格,并返回原始默認字符寬度。需要簡單說明的是:width是iostream類的一個成員函數。注意:width()只影響下一次操作,緊接著就變為默認值。?c++中字符寬度默認值是0。在輸出時真實字符寬度,會選擇設置字符寬度與完全顯示所需字符寬度,的最大值。
#include <iostream>int main()
{using std::cout;int w = cout.width(30);cout << "default field width = " << w << ":\n";cout.width(5);cout << "N" <<':';cout.width(8);cout << "N * N" << ":\n";for (long i = 1; i <= 100; i *= 10){cout.width(5);cout << i <<':';cout.width(8);cout << i * i << ":\n";}// std::cin.get();return 0;
}
填充字符
????????在默認情況下,cout用空格填充字段中未被使用的部分,可以用fill()成員函數來改變填充字符。cout.fill()會影響后續所有操作
#include <iostream>int main()
{using std::cout;cout.fill('*');const char * staff[2] = { "Waldo Whipsnade", "Wilmarie Wooper"};long bonus[2] = {900, 1350};for (int i = 0; i < 2; i++){cout << staff[i] << ": $";cout.width(7);cout << bonus[i] << "\n";}// std::cin.get();return 0;
}
設置浮點數顯示精度
? ? ? ? 這里的浮點數顯示精度是指:顯示的總位數。c++默認的浮點數顯示精度為6位,末尾的0不顯示。precision()成員函數用于修改浮點數顯示精度,末尾的0同樣不顯示。永久有效
#include <iostream>int main()
{using std::cout;float price1 = 20.40;float price2 = 1.9 + 8.0 / 9.0;cout << "\"Furry Friends\" is $" << price1 << "!\n";cout << "\"Fiery Fiends\" is $" << price2 << "!\n";cout.precision(2);cout << "\"Furry Friends\" is $" << price1 << "!\n";cout << "\"Fiery Fiends\" is $" << price2 << "!\n";cout.precision(7);cout << "\"Furry Friends\" is $" << price1 << "!\n";cout << "\"Fiery Fiends\" is $" << price2 << "!\n";// std::cin.get();return 0;
}
打印末尾的0和小數點
? ? ? ? 格式控制符:showpoint,用于顯示末尾的0和小數點,永久有效。
#include <iostream>int main()
{using std::cout;// using std::ios_base;float price1 = 20.40;float price2 = 1.9 + 8.0 / 9.0;// cout.setf(ios_base::showpoint);不建議使用setfcout << showpoint;cout << "\"Furry Friends\" is $" << price1 << "!\n";cout << "\"Fiery Fiends\" is $" << price2 << "!\n";cout.precision(2);cout << "\"Furry Friends\" is $" << price1 << "!\n";cout << "\"Fiery Fiends\" is $" << price2 << "!\n";// std::cin.get();return 0;
}
其他格式控制符
? ? ? ? right--->設置為右對齊,永久生效
? ? ? ? c++默認就是右對齊
#include <iostream>
using namespace std;
int main()
{using std::cout;int a = 1234567;int b = 1;int c = 2;cout.width(7);cout << right;cout << b << endl;cout.width(7);cout << c << endl;cout.width(7);cout << a << endl;return 0;
}
? ? ? ? left--->設置為左對齊,永久生效
#include <iostream>
using namespace std;
int main()
{using std::cout;cout.fill('#');int a = 1234567;int b = 1;int c = 2;cout.width(7);cout << left;cout << b << endl;cout.width(7);cout << c << endl;cout.width(7);cout << a << endl;return 0;
}
????????fixed--->固定以小數顯示浮點數,永久生效
#include <iostream>
using namespace std;
int main()
{using std::cout;cout << fixed;double a = 0.000012;double b = 12345678.9;cout << a << endl;cout << b << endl;return 0;
}
????????scientific--->固定以科學計數法顯示浮點數,永久生效
#include <iostream>
using namespace std;
int main()
{using std::cout;double a = 0.12;double b = 12.9;cout << a << endl;cout << b << endl;cout << scientific;cout << a << endl;cout << b << endl;return 0;
}
? ? ? ? 其它格式控制符不再贅述。
? ? ? ? 大家很明顯感受到,用格式控制符來進行格式化輸出,更舒服一點。在前面的介紹中,cout.width()、cout.precision()、cout.fill()都是使用cout對象的成員函數來進行格式化輸出。在<iomanip>中為它們三個分別設置了相應的控制符。
<iomanip>
- setprecision()用于控制浮點數顯示精度,永久有效
- setfill()用于控制填充字符,永久有效
- setw()用于控制字符寬度,只生效一次
#include <iostream>
#include <iomanip>
#include <cmath>int main()
{using namespace std;// use new standard manipulatorscout << fixed << right;// use iomanip manipulatorscout << setw(6) << "N" << setw(14) << "square root"<< setw(15) << "fourth root\n";double root;for (int n = 10; n <=100; n += 10){root = sqrt(double(n));cout << setw(6) << setfill('.') << n << setfill(' ')<< setw(12) << setprecision(3) << root<< setw(14) << setprecision(4) << sqrt(root)<< endl;}// std::cin.get();return 0;
}