C++标准模板(STL)- 输入/输出操纵符-(std::get_money,std::put_money)

操纵符是令代码能以 operator<< 或 operator>> 控制输入/输出流的帮助函数。

不以参数调用的操纵符(例如 std::cout << std::boolalpha; 或 std::cin >> std::hex; )实现为接受到流的引用为其唯一参数的函数。 basic_ostream::operator<< 和 basic_istream::operator>> 的特别重载版本接受指向这些函数的指针。这些函数(或函数模板的实例化)是标准库中仅有的可取址函数。 (C++20 起)

以参数调用的操纵符(例如 std::cout << std::setw(10); )实现为返回未指定类型对象的函数。这些操纵符定义其自身的进行请求操作的 operator<< 或 operator>> 。

定义于头文件 <iomanip>
 


剖析货币值

std::get_money

template< class MoneyT >
/*unspecified*/ get_money( MoneyT& mon, bool intl = false );

(C++11 起)

用于表达式 in >> get_money(mon, intl) 中时,分析字符输入为 in 中当前感染的 locale 的 std::money_get 平面所指定的货币值,并存储结果于 mon

in >> get_money(mon, intl) 中的释出操作表现为有格式输入函数 (FormattedInputFunction) 。

参数

mon-要被写入货币值的变量。能为 long double 或 basic_string 之一
intl-若为 true 则期待找到要求的国际通货字符串,否则期待可选的通货符号

返回值

返回未指定类型对象,使得若 in 为 std::basic_istream<CharT, Traits> 类型输入流的名称,则表达式 in >> get_money(mon, intl) 表现为如同执行下列代码:

typedef std::istreambuf_iterator<CharT, Traits> Iter;
typedef std::money_get<CharT, Iter> MoneyGet;
std::ios_base::iostate err = std::ios_base::goodbit;
const MoneyGet &mg = std::use_facet<MoneyGet>(in.getloc());
 mg.get(Iter(in.rdbuf()), Iter(), intl, in, err, mon);
if (std::ios_base::goodbit != err)
     out.setstate(err);

调用示例 

GCC和libstdc的MinGW端口仅支持"C""POSIX"环境。

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>

int main()
{
    std::istringstream in("$1,234.56 2.22 USD  3.33");
    long double v1, v2;
    std::string v3;
    in.imbue(std::locale("en_US.UTF-8"));
    in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true);
    if (in)
    {
        std::cout << in.str() << " parsed as: "
                  << v1 << ", " << v2 << ", " << v3 << '\n';
    }
    else
    {
        std::cout << "Parse failed";
    }

    return 0;
}

输出

"$1,234.56 2.22 USD  3.33" parsed as: 123456, 222, 333

格式化并输出货币值

std::put_money

template< class MoneyT >
/*unspecified*/ put_money( const MoneyT& mon, bool intl = false );

(C++11 起)

用于表达式 out << put_money(mon, intl) 时,转换货币值 monout 中当前感染的 locale 的 std::money_put 平面所指定的字符表示。

out << put_money(mon, intl) 中的插入操作表现为有格式输出函数 (FormattedOutputFunction) 。

参数

mon-货币值, long double 或 std::basic_string 之一
intl-若为 true 则使用国际通货字符串,否则使用通货符号

返回值

返回未指定类型的对象,使若 out 为 std::basic_ostream<CharT, Traits> 类型输出流的名称,则表达式 out << put_money(mon, intl) 表现如同执行下列代码:

typedef std::ostreambuf_iterator<CharT, Traits> Iter;
typedef std::money_put<CharT, Iter> MoneyPut;
const MoneyPut& mp = std::use_facet<MoneyPut>(out.getloc());
const Iter end = mp.put(Iter(out.rdbuf()), intl, out, out.fill(), mon);
if (end.failed())
     out.setstate(std::ios::badbit);

调用示例

GCC和libstdc的MinGW端口仅支持"C""POSIX"环境。

#include <iostream>
#include <iomanip>

int main()
{
    long double mon = 123.45; // 或 std::string mon = "123.45";

    std::cout.imbue(std::locale("en_US.utf8"));
    std::cout << std::showbase
              << "en_US: " << std::put_money(mon)
              << " or " << std::put_money(mon, true) << std::endl;

    std::cout.imbue(std::locale("ru_RU.utf8"));
    std::cout << "ru_RU: " << std::put_money(mon)
              << " or " << std::put_money(mon, true) << std::endl;

    std::cout.imbue(std::locale("ja_JP.utf8"));
    std::cout << "ja_JP: " << std::put_money(mon)
              << " or " << std::put_money(mon, true) << std::endl;

    return 0;
}

输出

en_US: $1.23 or USD  1.23
ru_RU: 1.23 руб or 1.23 RUB 
ja_JP: ¥123 or JPY  123

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值