C++ cout的使用總結

cout 是C++中 ostream 類型的對象

cout 是C++中 ostream 類型的對象,該類被封裝在 < iostream > 庫中,該庫定義的名字都在命名空間 std 中,所以 cout 全稱是 std::cout 。

1、cout支持多種數據類型,如int、float、double、char、string等,它們都會被自動轉換成字符串進行輸出。

#include<bits/stdc++.h>
using namespace std;
int main(){
	int a = 1;
	bool b = true;
	double d = 1.2;
	char c = 'c';
	cout<<"a = "<<a<<endl<<"b = "<<b<<endl<<"c = "<<c<<endl<<"d = "<<d<<endl;
	return 0;
}

2、cout輸出的內容可以直接與文件流對象相連,實現將輸出內容寫入到文件中。例如:

std::cout<<std::endl;將刷新輸出緩衝區,確保輸出內容被立即顯示。

3、避免輸出時出現中文亂碼問題

爲了避免輸出時出現中文亂碼問題,可以使用std::wcout來輸出寬字符類型的字符串。同時,需要在編譯選項中加上-finput-charset=UTF-8來指定輸入文件的編碼格式。

1、cout以不同進制來輸出數字,默認是10進制,其他進制輸出方法如下:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
	int i = 90;
	cout << i << endl;
	cout << dec << i << endl; //dec 是十進制(效果和默認一樣)
	cout << oct << i << endl; //oct 是八進制輸出
	cout << hex << i << endl; //hex 是十六進制輸出(字母默認是小寫字母)
	cout << setiosflags(ios::uppercase);//setiosflags(ios::uppercase) 表示將字母大寫輸出,包含在庫 < iomanip > 中。
	cout << hex << i << endl; //hex 是十六進制輸出(大寫字母)
	cout << setbase(8) << i << endl;//setbase(n) 表示以 n 進制顯示,包含在庫 < iomanip > 中,n 只能取 8, 10, 16 三個值。
}

2、輸出數字位數的控制

保留幾位小數需要使用的頭文件是#include <iomanip>
cout<<fixed<<setprecision(2)<<1.23456<<endl;
示例代碼:

#include<bits/stdc++.h>
using namespace std;
int main(){
	cout<<fixed<<setprecision(2)<<1.23456<<endl;
	cout<<setprecision(2)<<fixed<<1.23456<<endl;
}

fixed和setprecision()先後順序不影響輸出結果

C++默認浮點數輸出有效位數是 6 位(若前面整數位數大於 6 位,使用科學計數法輸出),而通過以下幾種方式可以更改輸出精度:
1.使用 setprecision(n) 即可設置浮點數輸出的有效位數
(若前面整數位數大於 n 位,使用科學計數法輸出)
2.使用 setiosflags(ios::fixed) 或 fixed,表示對小數點後面數字的輸出精度進行控制
所以,和 setprecision(n) 結合使用即可設置浮點數小數點後面數字的輸出精度,位數不足的補零
以上均採用 “四捨五入” 的方法控制精度,三個控制符均包含在 std 命名空間中。
示例代碼:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
	double i = 123.456789;
	cout << i << endl;
	cout << setprecision(3) << i << endl;
	cout << setprecision(9) << i << endl;
	cout << setiosflags(ios::fixed);
	cout << i << endl;
	cout << fixed << setprecision(3) << i << endl;
	cout << setprecision(9) << fixed <<  i << endl;
}

3、顯示小數點和正負號showpointshowpos

示例代碼如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
	double a=5,b=-1.2;
	cout<<a<<"\t\t"<<b<<endl;
	cout<<showpoint<<a<<"\t\t"<<b<<endl;
	cout<<showpos<<a<<"\t"<<b<<endl;
}

另外一種寫法:

#include<bits/stdc++.h>
using namespace std;
int main(){
	double a=5,b=-1.2;
	cout<<a<<"\t\t"<<b<<endl;
	cout<<setiosflags(ios::showpoint);
	cout<<a<<"\t\t"<<b<<endl;
	cout<<setiosflags(ios::showpos);
	cout<<a<<"\t"<<b<<endl;
}

輸出結果

4、設置域寬和對齊方式setw()leftright

輸出默認右對齊

#include<bits/stdc++.h>
using namespace std;
int main(){
	int i = 10;
	//設置域寬爲3 
	cout<<setw(3)<<i<<endl;
	//設置域寬爲3,左對齊
	cout<<setw(3)<<left<<i<<endl;
	//設置域寬爲3,右對齊 
	cout<<setw(3)<<right<<i<<endl;
}

輸出結果:

其他方式:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int i = 10;
	//設置域寬爲3 
	cout<<setw(3)<<i<<endl;
	//設置域寬爲3,左對齊
	cout<<setiosflags(ios::left);
	cout<<setw(3)<<i<<endl;
	//設置域寬爲3,右對齊 
	cout<<setiosflags(ios::right);
	cout<<setw(3)<<i<<endl;
}

5、設置填充字符setfill()

示例代碼:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int hour = 14,minute = 18,second = 10;
	cout<<setw(2)<<setfill('0')<<hour<<":"<<minute<<":"<<second;
	return 0;
}

運行效果:

控制符 作用
setbase(n) 以n進制方式輸出(n=8,10,16)
setfill(ch) 設置字符填充,ch可以是字符常量或字符變量
setprecision(n) 設置輸出有效位數爲n位
setw(n) 設置字符寬度爲n位,只對後一個有影響
setiosflags(ios::uppercase) 以大寫字母顯示
setiosflags(ios::fixed) 實現對小數點後的數字的控制
setiosflags(ios::scientific) 以科學計數法顯示
setiosflags(ios::showpoint) 強制顯示小數點
setiosflags(ios::showpos) 強制顯示正號
setiosflags(ios::left) 設置輸出左對齊
setiosflags(ios::right) 設置輸出右對齊
resetiosflags(…) 終止括號中的輸出格式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章