
- C++ 基礎
- C++ 首頁
- C++ 概述
- C++ 環境設定
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基礎輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期和時間
- C++ 控制語句
- C++ 決策
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式和解構函式
- C++ 複製建構函式
- C++ 檔案處理
- C++ 檔案和流
- C++ 從檔案中讀取
C++ 基礎輸入/輸出
C++ 標準庫提供了廣泛的輸入/輸出功能,我們將在後續章節中介紹。本章將討論 C++ 程式設計所需的非常基本和最常見的 I/O 操作。
C++ I/O 發生在流中,流是位元組序列。如果位元組從鍵盤、磁碟驅動器或網路連線等裝置流向主記憶體,則稱為輸入操作;如果位元組從主記憶體流向顯示屏、印表機、磁碟驅動器或網路連線等裝置,則稱為輸出操作。
I/O 庫標頭檔案
以下是 C++ 程式中重要的標頭檔案:
序號 | 標頭檔案 & 函式和描述 |
---|---|
1 |
此檔案定義了cin、cout、cerr和clog物件,它們分別對應於標準輸入流、標準輸出流、無緩衝的標準錯誤流和帶緩衝的標準錯誤流。 |
2 |
此檔案聲明瞭用於使用所謂的引數化流運算子(例如setw和setprecision)執行格式化 I/O 的服務。 |
3 |
此檔案聲明瞭用於使用者控制的檔案處理的服務。我們將在檔案和流相關的章節中詳細討論它。 |
4 |
<bits/stdc++.h> 此標頭檔案包含大部分標準 C++ 庫,無需單獨指定每個庫即可新增廣泛的功能。這在編碼競賽中特別有用。 |
標準輸出流 (cout)
預定義物件cout是ostream類的例項。cout 物件被稱為“連線到”標準輸出裝置,通常是顯示屏。cout與流插入運算子一起使用,該運算子寫為<<(兩個小於號),如下例所示。
示例
#include <iostream> using namespace std; int main() { char str[] = "Hello C++"; cout << "Value of str is : " << str << endl; }
編譯並執行上述程式碼時,將產生以下結果:
Value of str is : Hello C++
C++ 編譯器還會確定要輸出的變數的資料型別,並選擇合適的流插入運算子來顯示該值。<< 運算子被過載以輸出內建型別整數、浮點數、雙精度數、字串和指標值的資料項。
插入運算子<<可以如上所示在一個語句中使用多次,並且endl用於在行尾新增換行符。
標準輸入流 (cin)
預定義物件cin是istream類的例項。cin 物件被稱為連線到標準輸入裝置,通常是鍵盤。cin與流提取運算子一起使用,該運算子寫為>>(兩個大於號),如下例所示。
示例
#include <iostream> using namespace std; int main() { char name[50]; cout << "Please enter your name: "; cin >> name; cout << "Your name is: " << name << endl; }
編譯並執行上述程式碼時,它將提示您輸入姓名。您輸入一個值,然後按 Enter 鍵檢視以下結果:
Please enter your name: cplusplus Your name is: cplusplus
C++ 編譯器還會確定輸入值的型別,並選擇合適的流提取運算子來提取該值並將其儲存到給定的變數中。
流提取運算子>>可以在一個語句中使用多次。要請求多個數據,可以使用以下方法:
cin >> name >> age;
這將等同於以下兩個語句:
cin >> name; cin >> age;
標準錯誤流 (cerr)
預定義物件cerr是ostream類的例項。cerr 物件被稱為連線到標準錯誤裝置,這也是顯示屏,但是cerr物件是無緩衝的,每次向 cerr 進行流插入都會立即顯示其輸出。
cerr也與流插入運算子一起使用,如下例所示。
示例
#include <iostream> using namespace std; int main() { char str[] = "Unable to read...."; cerr << "Error message : " << str << endl; }
編譯並執行上述程式碼時,將產生以下結果:
Error message : Unable to read....
標準日誌流 (clog)
預定義物件clog是ostream類的例項。clog 物件被稱為連線到標準錯誤裝置,這也是顯示屏,但是clog物件是有緩衝的。這意味著每次插入 clog 都會將其輸出儲存在緩衝區中,直到緩衝區已滿或緩衝區被重新整理。
clog也與流插入運算子一起使用,如下例所示。
示例
#include <iostream> using namespace std; int main() { char str[] = "Unable to read...."; clog << "Error message : " << str << endl; }
編譯並執行上述程式碼時,將產生以下結果:
Error message : Unable to read....
使用這些小的例子,您將無法看到 cout、cerr 和 clog 之間的任何區別,但是在編寫和執行大型程式時,區別會變得很明顯。因此,最好使用 cerr 流顯示錯誤訊息,而顯示其他日誌訊息時則應使用 clog。
使用 bits/stdc++ 進行輸入/輸出
**bits/stdc++.h** 是 C++ 中的非標準標頭檔案,因為它不屬於官方 C++ 標準庫。相反,它是一個 GCC 特定的標頭檔案,包含 C++ 中的大部分標準庫。您也可以在不使用其他庫的情況下使用 bits/stdc++.h 進行輸入和輸出。
示例
#include <bits/stdc++.h> using namespace std; int main() { int number; string name; // #include <iostream> // #include <string> cout << "Welcome to TutorialsPoint!" << endl; // Input of user's name and number cout << "Please enter your name: "; cin >> name; cout << "Please enter a number: "; cin >> number; cout << "Hello," << name << " You entered " << number << endl; // Demonstrating some STL features // #include <vector> vector<int> numbers; cout << "Enter 4 numbers: "; for (int i = 0; i < 4; ++i) { int temp; cin >> temp; numbers.push_back(temp); } cout << "You entered the following numbers: "; for (int num : numbers) { cout << num << " "; } cout << endl; // Sort and display the numbers // #include <algorithm> sort(numbers.begin(), numbers.end()); cout << "Sorted numbers: "; for (int num : numbers) { cout << num << " "; } cout << endl; return 0; }
編譯並執行上述程式碼時,將產生以下結果:
Welcome to TutorialsPoint! Please enter your name: Aman Please enter a number: 2006 Hello, Aman You 2006 Enter 4 numbers: 2 0 0 6 You entered the following numbers: 2 0 0 6 Sorted numbers: 0 0 2 6
因此,在這裡,我們不使用這些多個頭檔案,例如:
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
我們簡單地使用了<bits/stdc++.h>,因為它包含了輸入/輸出操作、字串處理、動態陣列和演算法所需的所有必要的標準庫,從而簡化了您的程式碼,使其更簡潔、更方便。它尤其用於競爭性程式設計。