【问题标题】:How to use std::num_put with a custom iterator?如何将 std::num_put 与自定义迭代器一起使用?
【发布时间】:2018-05-10 09:55:22
【问题描述】:

如何使用std::num_put 将数字写入自定义迭代器(例如std::back_insert_iterator<std::string>)?

std::string s;
using I = decltype(std::back_inserter(s));
auto& f = std::use_facet<std::num_put<char, I>>(std::locale());
f.put(std::back_inserter(s), /* what do I pass here? */, ' ', 5.6);

【问题讨论】:

    标签: c++ iterator std


    【解决方案1】:

    您可能不应该直接使用std::num_putstd::back_insert_iterator&lt;std::string&gt;。您的代码没有修改 locale 的行为,所以我不知道您要对方面做什么。

    相反,请考虑为 std::stringstream 注入您想要的任何方面,然后执行正常的流插入。

    std::stringstream ss;
    std::locale loc = // Some non-standard locale ?
    ss.imbue(loc); // if you have changed anything
    ss << 5.6;
    std::string s = ss.str();
    

    【讨论】:

    • 问题是我想使用不受字符串支持的自定义迭代器,back_insert_iterator 只是为了简化问题。
    • 如果您应该使用的唯一迭代器是ostreambuf_iterator,那么模板中的迭代器类型有什么意义?
    • @eyelash 包装 std::ostreambuf_iterator 的任何迭代器适配器,首先。你可以使用任何与你的迭代器无关的std::ios_base
    猜你喜欢
    • 1970-01-01
    • 2022-10-07
    • 2015-02-04
    • 2021-12-21
    • 2013-05-19
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多