名稱空間
變體
操作

std::money_get<CharT,InputIt>::get, do_get

來自 cppreference.com
< cpp‎ | locale‎ | money get
 
 
 
 
std::money_get
成員函式
money_get::getmoney_get::do_get
 
定義於標頭檔案 <locale>
public:

iter_type get( iter_type beg, iter_type end, bool intl, std::ios_base& str,

               std::ios_base::iostate& err, long double& units ) const;
(1)
iter_type get( iter_type beg, iter_type end, bool intl, std::ios_base& str,
               std::ios_base::iostate& err, string_type& digits ) const;
(2)
protected:

virtual iter_type do_get( iter_type beg, iter_type end, bool intl, std::ios_base& str,

                          std::ios_base::iostate& err, long double& units ) const;
(3)
virtual iter_type do_get( iter_type beg, iter_type end, bool intl, std::ios_base& str,
                          std::ios_base::iostate& err, string_type& digits ) const;
(4)

從輸入迭代器解析貨幣值並將結果寫入 long double 或字串。

1,2) 公有成員函式,呼叫最派生類的成員函式 do_get
3,4) 從輸入迭代器 beg 讀取字元,期望找到一個按照由 str.getloc() 中包含的 std::ctype facet(本頁面其餘部分稱作 ct)、str.getloc() 中包含的 std::moneypunct<CharT, intl> facet(本頁面其餘部分稱作 mp)以及從 str.flags() 獲取的流格式化標誌所指定規則格式化的貨幣值。

如果輸入迭代器 beg 在解析完成前變得等於 end,則在 err 中設定 failbiteofbit。如果解析因其他原因失敗,則在 err 中設定 failbit。無論哪種情況,在錯誤發生時都不會修改輸出引數(unitsdigits)。

如果解析成功,則不改變 err,並將結果儲存在 unitsdigits 中。

此函式使用的格式化 pattern 始終是 mp.neg_format()

如果 mp.grouping() 不允許千位分隔符,則遇到的第一個分隔符被視為解析錯誤,否則它們被視為可選。

如果 money_base::spacemoney_base::nonepattern 中的最後一個元素,則解析器在解析完貨幣值的其他元件後不會嘗試消耗任何空白。否則,在 money_base::space 出現的位置會消耗一個或多個空白字元。

如果在 str.flags() 中設定了 showbase 標誌,則需要貨幣符號或貨幣字串;如果未設定,則貨幣符號是可選的。

如果 mp.positive_sign()mp.negative_sign() 返回的字串的第一個字元在格式化模式的 money_base::sign 位置找到,則它被消耗,並且該字串的其餘字元在貨幣值的所有其他元件之後被期望和消耗。如果 mp.positive_sign()mp.negative_sign() 都非空,則符號是必需的,並且必須與其中一個字串的第一個字元匹配。如果其中一個字串為空,則符號是可選的(如果它不存在,則結果的符號對應於為空的字串)。如果兩個字串都為空,或具有相同的第一個字元,則結果被賦予正號。如果輸出引數是字串(digits)並且結果為負,則值 ct.widen('-') 作為結果的第一個字元儲存。

從輸入中提取的數字按照它們出現的順序放置在 digits 中(必要時透過 ct.widen() 進行加寬),或放入臨時緩衝區 buf1,從該緩衝區構建 units 的值,如同透過

static const char src[] = "0123456789-";
CharT atoms[sizeof(src)];
ct.widen(src, src + sizeof(src) - 1, atoms);
for (int i = 0; i < n; ++i)
buf2[i] = src[find(atoms, atoms+sizeof(src), buf1[i]) - atoms];
buf2[n] = 0;
sscanf(buf2, "%Lf", &units);

(其中 n 是從輸入中提取並存儲在 buf1 中的字元數,buf2 是另一個足夠大的字元緩衝區)。

目錄

[edit] 返回值

一個迭代器,指向識別為貨幣字串輸入的有效部分之後的最後一個字元。

[edit] 注意

貨幣單位假定為貨幣的最小非分數單位:美國是美分,日本是日元。因此,在美國 locale 中,輸入序列 "$1,056.23"units 中產生數字 105623.0,或在 digits 中產生字串 "105623"

因為如果 showbase 關閉,貨幣符號是可選的,但整個多字元 negative_sign() 是必需的,所以給定格式化模式 {sign, value, space, symbol},並且 showbase 關閉,negative_sign"-",字串 "-1.23 €" 解析為 -123 並在輸入流上留下未消耗的“€”,但如果 negative_sign"()",則字串 "(1.23 €)" 將被完全消耗。

I/O 運算子 std::get_money 為此函式提供了一個更簡單的介面。

[edit] 示例

#include <iostream>
#include <locale>
#include <sstream>
 
void demo_money_get(std::locale loc, const std::string& input)
{
    std::istringstream str(input);
    str.imbue(loc);
    long double units;
 
    // The following can be written simpler with std::get_money(units)
    std::ios_base::iostate err = std::ios_base::goodbit;
    std::istreambuf_iterator<char> ret =
        std::use_facet<std::money_get<char>>(loc).get(
            std::istreambuf_iterator<char>(str),
            std::istreambuf_iterator<char>(),
            false, str, err, units);
    str.setstate(err);
    std::istreambuf_iterator<char> last{};
    if (str)
    {
        std::cout << "Successfully parsed '" << str.str() << "' as "
                  << units / 100 << " units\n";
        if (ret != last)
        {
            std::cout << "Remaining content: '";
            std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
            std::cout << "'\n";
        }
        else
            std::cout << "The input was fully consumed\n";
    }
    else
    {
        std::cout << "Parse failed. Unparsed string: '";
        std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
        std::cout << "'\n";
    }
}
 
int main()
{
    demo_money_get(std::locale("en_US.utf8"), "-$5.12 abc");
    demo_money_get(std::locale("ms_MY.utf8"), "(RM5.12) def");
}

輸出

Successfully parsed '-$5.12 abc' as -5.12 units
Remaining content: ' abc'
Successfully parsed '(RM5.12) def' as -5.12 units
Remaining content: ' def'

[edit] 參閱

定義 std::money_getstd::money_put 使用的貨幣格式化引數
(類模板) [edit]
從輸入字元序列解析和構造貨幣值
(類模板) [edit]
(C++11)
解析貨幣值
(函式模板) [edit]