public member function
<locale>

std::money_get::get

numerical (1)
iter_type get (iter_type s, iter_type end, bool intl, ios_base& str,               ios_base::iostate& err, long double& units) const;
string (2)
iter_type get (iter_type s, iter_type end, bool intl, ios_base& str,               ios_base::iostate& err, string_type& digits) const;
讀取貨幣表示式
解析 send 之間的字元序列作為貨幣表示式,並將其儲存到 unitsdigits 中。

該函式會讀取字元,直到讀取的字元不再是有效序列的一部分,或者 end 被到達。函式返回的迭代器將指向最後處理的字元之後的字元。

如果成功,第一個版本(1)會將對貨幣表示式的解釋後的浮點值儲存到 units 中。第二個版本(2)會將提取的數字作為 basic_string 物件儲存到 digits 中。

如果需要,該函式會更新 err 的錯誤狀態。
err 中的值描述
不變成功(未到達 end)。
failbit失敗:序列未匹配預期的格式
eofbit操作期間到達了 end(這可能發生在成功或失敗的情況下)。

在內部,該函式只是呼叫了虛保護成員函式 do_get,該函式預設情況下會解析貨幣表示式,並考慮 str格式標誌。該函式使用 str 中選定的區域設定來獲取格式詳細資訊(透過 moneypunct<char_type,intl> facet)以及對映字元(透過 ctype<char_type> facet)。

引數

s, end
指向序列開始和結束字元的迭代器。使用的範圍是 [s,end),其中包含 send 之間的所有字元,包括 s 指向的字元,但不包括 end 指向的字元。
成員型別 iter_type 是 facet 的迭代器型別(定義為 money_get 的第二個模板引數 InputIterator 的別名)。預設情況下,這是 istreambuf_iterator,允許從 basic_istream 物件進行隱式轉換。
intl
true 表示國際表示,否則為 false
這用於選擇 moneypunct 的正確例項化。
str
派生自 ios_base 的類物件(通常是輸入流物件)。它僅用於獲取格式資訊。
err
流錯誤狀態物件,型別為 ios_base::iostate,錯誤狀態在設定時儲存在此處。
units
該函式將讀取的貨幣表示式的浮點值等效值儲存在此變數中。
digits
該函式會將成功提取的數字(僅數字,不包含標點符號)儲存在此變數中,型別為string
成員型別 string_typebasic_string 的一個例項化,其字元型別與 facet 相同(定義為 basic_string<charT> 的別名,其中 charTmoney_get 的第一個模板引數)。

返回值

提取操作結束位置之後的下一個字元。
成員型別 iter_type 是 facet 的迭代器型別(定義為 money_get 的第二個模板引數 InputIterator 的別名)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// money_get example
#include <iostream>       // std::cin, std::cout, std::ios
#include <locale>         // std::locale, std::money_get, std::use_facet

int main ()
{
  std::locale loc("");    // default locale
  std::ios::iostate state;
  long double price;

  std::cout << "Please, enter the price: ";
  std::use_facet<std::money_get<char> >(loc).get
    (std::cin, std::money_get<char>::iter_type(), false, std::cin, state, price);

  if ((state & std::ios::failbit)==std::ios::failbit)
    std::cout << "ERROR: failed to read price\n";
  else
    std::cout << "The price of three items is " << (price*3.0) << '\n';

  return 0;
}

可能的輸出

Please, enter the price: $12.95
ERROR: failed to read price

預設區域設定無法解釋輸入的價位的格式。

資料競爭

訪問該物件以及 send 之間的整個範圍。
引數 strerrunits/digits 可能會被修改。

異常安全

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

另見