
輸出輸入程式庫 (input/output library) 提供各種輸入、輸出相關的內容,有以下的標頭檔
| 名稱 | 功能 |
|---|---|
| <iosfwd> | 基本的 I/O 類別。 |
| <ios> | std::ios_base class, std::basic_ios |
| <istream> | std::basic_istream |
| <ostream> | std::basic_ostream |
| <iostream> | std::basic_iostream |
| <fstream> | std::basic_fstream, std::basic_ifstream, std::basic_ofstream |
| <sstream> | std::basic_stringstream, std::basic_istringstream, std::basic_ostringstream |
| <strstream> | std::strstream, std::istrstream, std::ostrstream |
| <iomanip> | 控制 I/O 的幫助函數。 |
| <streambuf> | std::basic_streambuf |
| <cstdio> | C 語言風格的 I/O 函數。 |
舉一例如下
| 001 | #include <iostream> |
| 002 | |
| 003 | using namespace std; |
| 004 | |
| 005 | int main() { |
| 006 | int a; |
| 007 | int sum = 0; |
| 008 | |
| 009 | cin >> a; |
| 010 | sum += a; |
| 011 | |
| 012 | cin >> a; |
| 013 | sum += a; |
| 014 | |
| 015 | cin >> a; |
| 016 | sum += a; |
| 017 | |
| 018 | cout << "Sum: " |
| 019 | << sum |
| 020 | << endl; |
| 021 | |
| 022 | return 0; |
| 023 | } |
| 024 | |
| 025 | /* Kaiching Chang |
| 026 | u1506.cpp |
| 027 | 2014-02 */ |
此例示範 C++ 的 iostream ,因此要先 #include <iostream>
| 001 | #include <iostream> |
cin 為 iostream 中定義的輸入串流物件,後面的 >> 為輸入串流運算子
| 009 | cin >> a; |
而 cout 為 iostream 中定義的輸出串流物件,後面的 << 為輸入串流運算子
| 018 | cout << "Sum: " |
這個程式接收使用者輸入三個整數,然後印出相加的總和,編譯執行,結果如下
| $ g++ u1506.cpp |
| $ ./a.out |
| 12 |
| 23 |
| 34 |
| Sum: 69 |
| $ |
continue ...
沒有留言:
張貼留言