名稱空間
變體
操作

std::money_get

來自 cppreference.com
< cpp‎ | 本地化
 
 
 
 
 
定義於標頭檔案 <locale>
模板<

    CharT,
    InputIt = std::istreambuf_iterator<CharT>

> money_get;

類模板 std::money_get 封裝了從字元流中解析貨幣值的規則。標準 I/O 運算子 std::get_money 使用 I/O 流本地化設定中的 std::money_get 方面。

cpp/locale/locale/facetstd-money get-inheritance.svg

繼承圖

如果標準庫不保證提供 std::money_get 特化(見下文),則不保證其 get()do_get() 的行為符合規範。

目錄

[編輯] 特化

標準庫保證提供以下特化(它們 要求由任何區域設定物件實現

定義於標頭檔案 <locale>
std::money_get<char> 解析貨幣值的窄字串表示
std::money_get<wchar_t> 解析貨幣值的寬字串表示

此外,標準庫還保證提供滿足以下型別要求的每個特化:

[編輯] 巢狀型別

型別 定義
char_type CharT
string_type std::basic_string<CharT>
iter_type InputIt

[編輯] 資料成員

成員 描述
std::locale::id id [靜態] facet 的識別符號

[編輯] 成員函式

構造一個新的 money_get 方面
(公開成員函式)
呼叫 do_get
(公開成員函式)

[編輯] 受保護成員函式

析構一個 money_get 方面
(受保護成員函式)
[虛擬]
從輸入流中解析貨幣值
(虛擬受保護成員函式) [編輯]

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
 
int main()
{
    std::string str = "$1.11 $2.22 $3.33";
    std::cout << std::fixed << std::setprecision(2);
 
    std::cout << '\"' << str << "\" parsed with the I/O manipulator: ";
    std::istringstream s1(str);
    s1.imbue(std::locale("en_US.UTF-8"));
 
    long double val;
    while (s1 >> std::get_money(val))
        std::cout << val / 100 << ' ';
    std::cout << '\n';
 
    str = "USD  1,234.56";
    std::cout << '\"' << str << "\" parsed with the facet directly: ";
    std::istringstream s2(str);
    s2.imbue(std::locale("en_US.UTF-8"));
 
    auto& f = std::use_facet<std::money_get<char>>(s2.getloc());
    std::ios_base::iostate err;
    std::istreambuf_iterator<char> beg(s2), end;
    f.get(beg, end, true, s2, err, val);
 
    std::cout << val / 100 << '\n';
}

輸出

"$1.11 $2.22 $3.33" parsed with the I/O manipulator: 1.11 2.22 3.33
"USD  1,234.56" parsed with the facet directly: 1234.56

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 427 C++98 money_get 保證接受任何 CharT
滿足作為字元的要求
任何 iostream 元件都可以在其上例項化
只保證接受 char,
wchar_t 和其他實現-
定義的字元型別
LWG 2392 C++98 只有字元型別 CharT 可以
保證被 money_get 接受
可以保證接受實現-
定義的字元容器型別

[編輯] 另請參閱

定義由 std::money_getstd::money_put 使用的貨幣格式化引數
(類模板) [編輯]
將貨幣值格式化為字元序列輸出
(類模板) [編輯]
(C++11)
解析貨幣值
(函式模板) [編輯]