std::basic_istream<CharT,Traits>::operator>>
來自 cppreference.com
< cpp | io | basic istream
basic_istream& operator>>( unsigned short& value ); |
(1) | |
basic_istream& operator>>( unsigned int& value ); |
(2) | |
basic_istream& operator>>( long& value ); |
(3) | |
basic_istream& operator>>( unsigned long& value ); |
(4) | |
basic_istream& operator>>( long long& value ); |
(5) | (C++11 起) |
basic_istream& operator>>( unsigned long long& value ); |
(6) | (C++11 起) |
basic_istream& operator>>( float& value ); |
(7) | |
basic_istream& operator>>( double& value ); |
(8) | |
basic_istream& operator>>( long double& value ); |
(9) | |
basic_istream& operator>>( bool& value ); |
(10) | |
basic_istream& operator>>( void*& value ); |
(11) | |
basic_istream& operator>>( short& value ); |
(12) | |
basic_istream& operator>>( int& value ); |
(13) | |
basic_istream& operator>>( /* extended-floating-point-type */& value ); |
(14) | (C++23 起) |
basic_istream& operator>>( std::ios_base& (*func)(std::ios_base&) ); |
(15) | |
basic_istream& operator>>( std::basic_ios<CharT, Traits>& (*func)(std::basic_ios<CharT, Traits>&) ); |
(16) | |
basic_istream& operator>>( basic_istream& (*func)(basic_istream&) ); |
(17) | |
basic_istream& operator>>( std::basic_streambuf<CharT, Traits>* sb ); |
(18) | |
從輸入流中提取值。
1-11) 提取一個值,可能跳過前面的空白字元。該值儲存到給定的引用 value 中。
此函式表現為 FormattedInputFunction。在構造並檢查崗哨物件(可能會跳過開頭的空白字元)之後,透過呼叫 std::num_get::get() 提取一個值。
12) 提取一個 short 值,可能跳過前面的空白字元。該值儲存到給定的引用 value 中。
此函式表現為 FormattedInputFunction。在構造並檢查崗哨物件(可能會跳過開頭的空白字元)之後,透過呼叫 std::num_get::get() 提取一個 long 值 lval。之後
- 如果 lval < std::numeric_limits<short>::min(),則設定
failbit
並將 std::numeric_limits<short>::min() 儲存到 val 中。 - 否則,如果 std::numeric_limits<short>::max() < lval,則設定
failbit
並將 std::numeric_limits<short>::max() 儲存到 val 中。 - 否則,將 static_cast<short>(lval) 儲存到 val 中。
13) 提取一個 int 值,可能跳過前面的空白字元。該值儲存到給定的引用 value 中。
此函式表現為 FormattedInputFunction。在構造並檢查崗哨物件(可能會跳過開頭的空白字元)之後,透過呼叫 std::num_get::get() 提取一個 long 值 lval。之後
- 如果 lval < std::numeric_limits<int>::min(),則設定
failbit
並將 std::numeric_limits<int>::min() 儲存到 val 中。 - 否則,如果 std::numeric_limits<int>::max() < lval,則設定
failbit
並將 std::numeric_limits<int>::max() 儲存到 val 中。 - 否則,將 static_cast<int>(lval) 儲存到 val 中。
確定標準浮點型別
FP
如下:- 如果 /* extended-floating-point-type */ 的 浮點轉換等級 小於或等於 float 的等級,則
FP
為 float。 - 否則,如果 /* extended-floating-point-type */ 的浮點轉換等級小於或等於 double 的等級,則
FP
為 double。 - 否則,
FP
為 long double。
此函式表現為 FormattedInputFunction。在構造並檢查崗哨物件(可能會跳過開頭的空白字元)之後,透過呼叫 std::num_get::get() 提取一個
FP
值 fval。之後- 如果 fval < -std::numeric_limits</* extended-floating-point-type */>::max(),則設定
failbit
並將 -std::numeric_limits</* extended-floating-point-type */>::max() 儲存到 val 中。 - 否則,如果 std::numeric_limits</* extended-floating-point-type */>::max() < fval,則設定
failbit
並將 std::numeric_limits</* extended-floating-point-type */>::max() 儲存到 val 中。 - 否則,將 static_cast</* extended-floating-point-type */>(fval) 儲存到 val 中。
15-17) 呼叫 func(*this),其中 func 是一個 I/O 操縱符。
18) 表現為 UnformattedInputFunction。在構造並檢查崗哨物件之後,從 *this 提取所有資料並將其儲存到 sb 中。當滿足以下條件之一時,提取停止:
- 輸入序列中出現檔案結束;
- 插入到輸出序列中失敗(在這種情況下,要插入的字元不會被提取);
- 發生異常(在這種情況下,異常被捕獲,並且只有在未插入任何字元且
exceptions()
中啟用了failbit
時才重新丟擲)。
如果提取失敗(例如,如果在預期數字的地方輸入了字母),則會將零寫入 value 並設定 failbit
。對於有符號整數,如果提取的值太大或太小,無法放入 value 中,則會寫入 std::numeric_limits<T>::max() 或 std::numeric_limits<T>::min()(分別)並設定 failbit
標誌。對於無符號整數,如果提取的值太大或太小,無法放入 value 中,則會寫入 std::numeric_limits<T>::max() 並設定 failbit
標誌。
目錄 |
[編輯] 引數
value | - | 對整數或浮點值的引用,用於儲存提取的值 |
func | - | 指向 I/O 操縱符函式的指標 |
sb | - | 指向流緩衝區的指標,所有資料都將寫入其中 |
[編輯] 返回值
1-16,18) *this
17) func(*this)
[編輯] 注意
對於過載 (14),當擴充套件浮點型別的浮點轉換等級與任何標準浮點型別的等級不相等時,轉換期間的雙重舍入可能導致不準確的結果。std::from_chars() 可用於需要最大精度的情況。
[編輯] 示例
執行此程式碼
#include <iomanip> #include <iostream> #include <sstream> int main() { std::string input = "41 3.14 false hello world"; std::istringstream stream(input); int n; double f; bool b; stream >> n >> f >> std::boolalpha >> b; std::cout << "n = " << n << '\n' << "f = " << f << '\n' << "b = " << std::boolalpha << b << '\n'; // extract the rest using the streambuf overload stream >> std::cout.rdbuf(); std::cout << '\n'; }
輸出
n = 41 f = 3.14 b = false hello world
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 64 | C++98 | 不清楚過載 (18) 是否只能重新丟擲 呼叫 setstate(failbit) 丟擲的 std::ios_base::failure |
所有捕獲的異常 都可以重新丟擲 |
LWG 118 | C++98 | 過載 (12,13) 將提取委託給 num_get::get, 但它沒有 short 和 int 的過載 |
提取一個 long 值 而不是 short 或 int |
LWG 413 | C++98 | 過載 (18) 只重新丟擲在從 sb 提取字元時丟擲的異常 但是字元是從 *this 提取的 |
將 sb 更正為 *this |
LWG 567 | C++98 | 過載 (18) 表現為 FormattedInputFunction 由於 LWG issue 60 的決議 |
它表現為 UnformattedInputFunction(非格式化輸入函式) |
LWG 661 | C++98 | 過載 (12,13) 未將提取的數字儲存到 value 中,由於 LWG issue 118 的解決 |
如果未發生溢位,則儲存該數字 未發生溢位 |
LWG 696 | C++98 | 在提取失敗時,value 未改變 | 設定為零或最小值/ 最大值 |
[編輯] 另請參閱
提取字元和字元陣列 (函式模板) | |
對字串執行流輸入和輸出 (函式模板) | |
執行位集的流輸入和輸出 (函式模板) | |
序列化和反序列化複數 (函式模板) | |
(C++11) |
對偽隨機數引擎執行流輸入和輸出 (函式模板) |
(C++11) |
對偽隨機數分佈執行流輸入和輸出 (函式模板) |
提取字元塊 (公共成員函式) | |
提取已有的字元塊 (公共成員函式) | |
提取字元 (公共成員函式) | |
提取字元直到找到給定字元 (公共成員函式) | |
(C++17) |
將字元序列轉換為整數或浮點值 (函式) |