標準I/O串流
I/O串流
- 在C++中,將資料從一個對象到另一個對象的流動抽象為“串流”。串流在使用前要被建立,使用後要被刪除。
- 從串流中獲取資料的操作稱為提取操作,向串流中添加資料的操作稱為插入操作。
- 資料的輸入與輸出是透過I/O串流來實現的,cin和cout是預定義的串流類對象。cin用來處理標準輸入,即鍵盤輸入。cout用來處理標準輸出,即螢幕輸出。
預定義的插入符和提取符
- “<<”是預定義的插入符,作用在串流類對象cout上可以實現向標準輸出設備輸出。
- 例如:
cout << 表達式 << 表達式...
- 例如:
- 標準輸入是將提取符作用在串流類對象cin上。
- 例如:
cin >> 表達式 >> 表達式...
- 例如:
- 提取符號可以連續使用,每個提取符號後面跟隨一個表達式,該表達式通常是用來儲存輸入值的變數。例如:
- int a, b;
- cin >> a >> b;
C++字元集
- 大小寫英文字母:A~Z,a~z
- 數字字元:0~9
- 特殊字元:
! # % ^ & * _ + = - ~ < > / \ ‘ “ ; . : ? ( ) [ ] { } |
詞法記號
- 關鍵字: C++預定義的單字
- 識別符: 程式設計師聲明的單字,用來命名程式正文中的一些實體
- 文字: 在程式中直接使用符號表示的資料
- 運算符: 用於實現各種運算的符號
- 分隔符:
() {} : ;
用於分隔各個詞法記號或程式正文 - 空白符: 空格、制表符(TAB鍵產生的字元)、垂直制表符、換行符、回車符和註解的總稱
關鍵字
以下是C++的部分關鍵字:
alignas alignof asm auto bool break case catch char char16_t char32_t class const constexpr const_cast continue decltype default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new noexcept nullptr operator private protected public reinterpret_cast return short signed sizeof static static_cast struct switch template this thread_local throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while
識別符的構成規則
- 以大寫字母、小寫字母或底線(_)開始。
- 可以由大寫字母、小寫字母、底線(_)或數字0~9組成。
- 大寫字母和小寫字母代表不同的識別符。
- 不能是C++關鍵字或運算符。