c ++中的基本輸入輸出
標準庫 <iostream> 定義了幾個輸入和輸出流:
|stream | description |
|-------|----------------------------------|
|cin | standard input stream |
|cout | standard output stream |
|cerr | standard error (output) stream |
|clog | standard logging (output) stream |
在上面提到的四個流中,cin 基本上用於使用者輸入,而其他三個用於輸出資料。通常或在大多數編碼環境中,cin( 控制檯輸入或標準輸入)是鍵盤,cout( 控制檯輸出或標準輸出)是監視器。
cin >> value
cin - input stream
'>>' - extraction operator
value - variable (destination)
cin 這裡提取使用者輸入的輸入並以變數值輸入。僅在使用者按 ENTER 鍵後才提取該值。
cout << "Enter a value: "
cout - output stream
'<<' - insertion operator
"Enter a value: " - string to be displayed
cout 此處顯示要顯示的字串並將其插入標準輸出或監視器
所有四個流都位於標準名稱空間 std 中,因此我們需要為 stream 列印 std::stream 才能使用它。
程式碼中還有一個操作器 std::endl。它只能用於輸出流。它在流中插入行尾'\n'字元並將其重新整理。它會立即產生輸出。