C++ 學習筆記之——輸入和輸出
阿新 • • 發佈:2018-10-29
根據 也會 指向 http 對象 是否 取消 成員函數 輸入輸出
在 C++ 中,我們通過調用輸入輸出流庫中的流對象 cin 和 cout 來實現輸入和輸出。
#include <iostream> using namespace std; int main() { int a = 0; float b = 0; char c = 0; cin >> a >> b >> c; cout << a << ‘\t‘ << b << ‘\t‘ << c << ‘\t‘ << endl; return 0; }
56 Enter
5.36 Enter
a Enter
56 5.36 a
在用 cin 進行輸入時,我們不用指定具體的數據類型,系統會根據變量的類型從輸入流中提取相應長度的字節。同樣,用 cout 進行輸出時,系統也會自動判別輸出數據的類型使輸出的數據按相應的類型輸出。
通過 cin 讀入數據時,會自動跳過輸入流中的空格、tab 鍵和換行字符。當遇到無效字符或者文件結束符時,輸入 cin 就會處於出錯狀態,我們可以通過判斷 cin 的值來判斷流對象是否處於正常狀態和提取操作是否成功。
另外,我們還可以使用控制符來設置輸入輸出的格式。
#include <iostream> #include <iomanip> using namespace std; int main() { int a = 256; float b = 3.1415926525; cout << b << endl; cout << setprecision(9) << b << endl; // 設置精度 cout << setiosflags(ios::fixed) << setprecision(2) << b << endl; // 設置小數點位數 cout << endl << a << endl; cout << hex << a << endl; // 十六進制輸出 cout << dec << setfill(‘*‘) << setw(5) << a << endl; // 設置域寬和填充 cout << setiosflags(ios::showpos) << a << endl; // 顯示符號 cout << resetiosflags(ios::showpos) << a << endl; // 取消顯示符號 return 0; }
3.14159
3.14159274
3.24
256
100
**256
+256
256
也可以使用 cout 的成員函數來設置輸入輸出的格式。
#include <iostream> using namespace std; int main() { int a = 256; float b = 3.1415926525; cout << b << endl; cout.precision(9); // 設置精度 cout << b << endl; cout.precision(2); cout.setf(ios::fixed); // 設置固定小數點位數 cout << b << endl; cout << endl << a << endl; cout.setf(ios::hex, ios::basefield); cout << a << endl; cout.fill(‘*‘); // 設置填充 cout.width(5); // 設置域寬 cout << a << endl; cout.setf(ios::showpos); // 顯示符號 cout << a << endl; cout.unsetf(ios::showpos); // 取消顯示符號 cout << a << endl; return 0; }
3.14159
3.14159274
3.24
256
100
**256
+256
256
為了與 C 語言兼容,C++ 保留了 C 中的 scanf 和 printf 函數進行輸入輸出,以及 getchar 和 putchar 函數進行單個字符的輸入輸出。
此外,我們還可以用輸入輸出流對象的一些成員函數來實現輸入和輸出。
cout.put()
輸出單個字符,可以連續輸出cin.get()
讀入一個字符(包括空白字符),返回讀入成功的字符,如遇到文件結束符,返回 EOFcin.get(ch)
讀入一個字符並賦值給變量 ch,成功讀入則返回真cin.get
(字符數組或指針,字符個數 n,終止字符) 讀入 n-1 個字符,如遇到終止字符則提前結束cin.getline
(字符數組或指針,字符個數 n,終止字符) 與上面的 cin.get 類似,但是遇到終止字符時,字符指針會移到該終止字符後面,而 cin.get 則會停留在原位置
#include <iostream>
using namespace std;
int main()
{
char ch[20] = {0};
cin >> ch[0]; // 讀入 1
cout.put(ch[0]);
cout << endl;
ch[1] = cin.get(); // 讀入 2
cout.put(ch[1]);
cout << endl;
cin.get(ch[2]); // 讀入 3
cout.put(ch[2]);
cout << endl;
cin.get(ch, 20, ‘/‘); // 讀入 123 之後的第一個回車以及 ‘hello, seniusen! h‘ 共計 19 個字符
cout << ch << endl;
cin.get(ch, 20, ‘/‘); // 讀入‘ello, seniusen!‘ 共計 15 個字符,遇到終止字符 ‘/’ 停止
cout << ch << endl;
cin.getline(ch, 20, ‘/‘); // 當前字符指針還停留在字符 ‘/’ 處,直接停止讀入,字符指針後移一位指向空格
cout << ch << endl;
cin.getline(ch, 20, ‘/‘); // 讀入‘ hello, seniusen!‘ 共計 17 個字符,遇到終止字符 ‘/’ 停止
cout << ch << endl;
return 0;
}
123 Enter
1
2
3
hello, seniusen! hello, seniusen!/ hello, seniusen!/ Enter
hello, seniusen! h
ello, seniusen!
hello, seniusen!
一些其他的成員函數:
cin.eof()
如果到達文件末尾(遇文件終止符)返回真,否則返回假cin.peek()
返回當前指針指向的字符,但只是觀測,指針仍然停留在當前位置cin.putback(ch)
將字符 ch 返回到輸入流,插入到當前指針位置cin.ignore
(n, 終止字符) 跳過輸入流中 n 個字符,若遇到終止符提前結束,此時指向終止字符後面一個位置
獲取更多精彩,請關註「seniusen」!
C++ 學習筆記之——輸入和輸出