public member function
<locale>

std::num_get::get

bool (1)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, bool& val) const;
long (2)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, long& val) const;
unsigned short (3)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, unsigned short& val) const;
unsigned int (4)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, unsigned int& val) const;
unsigned long (5)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, unsigned long& val) const;
float (6)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, float& val) const;
double (7)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, double& val) const;
long double (8)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, long double& val) const;
pointer (9)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, void*& val) const;
bool (1)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, bool& val) const;
long (2)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, long& val) const;
unsigned short (3)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, unsigned short& val) const;
unsigned int (4)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, unsigned int& val) const;
unsigned long (5)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, unsigned long& val) const;
float (6)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, float& val) const;
double (7)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, double& val) const;
long double (8)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, long double& val) const;
pointer (9)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, void*& val) const;
long long (10)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, long long& val) const;
unsigned long long (11)
iter_type get (iter_type in, iter_type end, ios_base& str,               ios_base::iostate& err, unsigned long long& val) const;
Get numeric value
解析 inend 之間的字元序列作為數值,並將其儲存到 v 中。為此,它使用 str 中選擇的格式選項(透過其 ios_base::fmtflags 值),並在必要時使用錯誤狀態更新 err

該函式在序列中的一個字元不能構成有效的數值表示式(或到達 end)時停止讀取字元。序列中的下一個字元由函式返回的迭代器指向。

一個 ios_base::iostate 位掩碼值儲存在 err 中,表示操作的結果。
err 中的值value in val描述
goodbitthe value read成功(未到達 end)。
failbitunspecified失敗:字元序列不符合預期格式。
eofbitone of the above操作期間到達了 end(無論成功還是失敗)。
err 中的值value in val描述
unchangedthe value read成功(未到達 end)。
failbit序列不符合預期格式。
numeric_limits::max()序列表示的值對於 val 的型別來說過大。
numeric_limits::lowest()序列表示的負值對於 val 的型別來說過大。
eofbitone of the above操作期間到達了 end(無論成功還是失敗)。

該函式在內部呼叫虛保護成員 do_get,該成員預設解析數值,其格式與 scanf 解析 val 引數型別對應的*格式說明符*的格式相同,同時考慮 str 的*格式標誌*。該函式使用 str 中選定的區域設定(透過 numpunct 構面)獲取格式詳細資訊,並使用 ctype::widen 來擴充套件字元。

引數

in, end
指向序列開始和結束字元的迭代器。使用的範圍是 [in,end),它包含 inend 之間的所有字元,包括 in 指向的字元,但不包括 end 指向的字元。
成員型別 iter_type 是構面的迭代器型別(定義為 num_get 的第二個模板引數 InputIterator 的別名)。預設情況下,它是一個 istreambuf_iterator,允許從 basic_istream 物件隱式轉換。
str
派生自 ios_base 的類物件(通常是輸入流物件)。它僅用於獲取格式資訊。
err
流錯誤狀態物件,型別為 ios_base::iostate,結果狀態儲存在此物件中。
val
數值型別的元素。
如果函式成功提取了數字(即 errfailbit 標誌未設定),則將值儲存在此處。

返回值

提取操作結束後的序列中的下一個字元。
成員型別 iter_type 是構面的迭代器型別(定義為 num_get 的第二個模板引數 InputIterator 的別名)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// num_get example
#include <iostream>       // std::cin, std::cout, std::ios
#include <locale>         // std::locale, std::num_get, std::use_facet
#include <iterator>       // std::istreambuf_iterator
#include <string>         // std::string

int main ()
{
  std::locale loc;
  std::ios::iostate state;
  float mypi,yourpi;

  std::string number ("3.14159");
  // get from string:
  std::use_facet<std::num_get<char,std::string::iterator> > (loc).get
    (number.begin(), number.end(), std::cin, state, mypi);

  std::cout << "Please, enter PI: ";
  // get from istream:
  std::use_facet<std::num_get<char> >(loc).get
    (std::cin, std::istreambuf_iterator<char>(), std::cin, state, yourpi);

  if ( (mypi-yourpi>0.01) || (mypi-yourpi<-0.01) )
    std::cout << "Wrong!\n";
  else
    std::cout << "Right!\n";

  return 0;
}

可能的輸出

Please, enter PI: 3.14
Right!


資料競爭

物件以及 inend 之間的整個範圍都會被訪問。
strerrval 引數可能會被修改。

異常安全

如果丟擲異常,則“facet 物件”不會發生更改,儘管某些引數可能已被修改。

另見