fmt::basic_format_context

里面保存了OutputIt,其实就是std::back_insert_iterator的一个子类,见

C++ STL insert_iterator插入迭代器适配器(深入了解,一文学会)-CSDN博客

 用等于号(=),内部会调用push_back

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
 
//创建一个 vector 容器
vector<int> foo;
//创建一个可向 foo 容器尾部添加新元素的迭代器
back_insert_iterator< vector<int> > back_it(foo);
//将 5 插入到 foo 的末尾
back_it = 5;
//将 4 插入到当前 foo 的末尾
back_it = 4;
//将 3 插入到当前 foo 的末尾
back_it = 3;
//将 6 插入到当前 foo 的末尾
back_it = 6;
//输出 foo 容器中的元素
for (vector<int>::iterator it = foo.begin(); it != foo.end(); ++it)
	cout << *it << ' ';
// Formatting context.
template <typename OutputIt, typename Char> class basic_format_context {
 public:
  /** The character type for the output. */
  using char_type = Char;

 private:
  OutputIt out_;
  basic_format_args<basic_format_context> args_;
  detail::locale_ref loc_;

 public:
  using iterator = OutputIt;
  using format_arg = basic_format_arg<basic_format_context>;
  using parse_context_type = basic_format_parse_context<Char>;
  template <typename T> using formatter_type = formatter<T, char_type>;

  basic_format_context(basic_format_context&&) = default;
  basic_format_context(const basic_format_context&) = delete;
  void operator=(const basic_format_context&) = delete;
  /**
   Constructs a ``basic_format_context`` object. References to the arguments are
   stored in the object so make sure they have appropriate lifetimes.
   */
   basic_format_context(
      OutputIt out, basic_format_args<basic_format_context> ctx_args,
      detail::locale_ref loc = detail::locale_ref())
      : out_(out), args_(ctx_args), loc_(loc) {}

   auto arg(int id) const -> format_arg { return args_.get(id); }
   auto arg(basic_string_view<char_type> name) -> format_arg {
    return args_.get(name);
  }
   auto arg_id(basic_string_view<char_type> name) -> int {
    return args_.get_id(name);
  }
  auto args() const -> const basic_format_args<basic_format_context>& {
    return args_;
  }

   auto error_handler() -> detail::error_handler { return {}; }
  void on_error(const char* message) { error_handler().on_error(message); }

  // Returns an iterator to the beginning of the output range.
   auto out() -> iterator { return out_; }

  // Advances the begin iterator to ``it``.
  void advance_to(iterator it) {
    if (!detail::is_back_insert_iterator<iterator>()) out_ = it;
  }

   auto locale() -> detail::locale_ref { return loc_; }
};

会调用到advance_to这个函数

  // Advances the begin iterator to ``it``.
  void advance_to(iterator it) {
    if (!detail::is_back_insert_iterator<iterator>()) out_ = it;
  }

其中, out_ = it; operator = 内部调用的是push_back。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值