back_insert_iterator源码分析

back_insert_iterator源码分析

gcc 4.1.2 版本的 back_insert_iterator


  // 24.4.2.2.1 back_insert_iterator
  /**
   *  @brief  Turns assignment into insertion.
   *
   *  These are output iterators, constructed from a container-of-T.
   *  Assigning a T to the iterator appends it to the container using
   *  push_back.
   *
   *  Tip:  Using the back_inserter function to create these iterators can
   *  save typing.
  */
  template<typename _Container>
    class back_insert_iterator
    : public iterator<output_iterator_tag, void, void, void, void>
    {
    protected:
      _Container* container;

    public:
      /// A nested typedef for the type of whatever container you used.
      typedef _Container          container_type;

      // 非常巧妙的使用 explicit 限制了初始化必须传入类型,这个以后再编写类似
      // 的专用类的时候,可以参考这样的实现
      // 构造函数传入必须是一个容器
      /// The only way to create this %iterator is with a container.
      explicit
      back_insert_iterator(_Container& __x) : container(&__x) { }

      /**
       *  @param  value  An instance of whatever type
       *                 container_type::const_reference is; presumably a
       *                 reference-to-const T for container<T>.
       *  @return  This %iterator, for chained operations.
       *
       *  This kind of %iterator doesn't really have a "position" in the
       *  container (you can think of the position as being permanently at
       *  the end, if you like).  Assigning a value to the %iterator will
       *  always append the value to the end of the container.
      */
      //将赋值运算符适配成 push_back, 返回值为 back_insert_iterator 迭代器适配器
      //传入容器必须要还有 push_back接口(常用容器都是有这个接口)
      back_insert_iterator&
      operator=(typename _Container::const_reference __value)
      {
    container->push_back(__value);
    return *this;
      }

      /// Simply returns *this.
      //空操作,保证迭代器接口的完备
      back_insert_iterator&
      operator*()
      { return *this; }

      /// Simply returns *this.  (This %iterator does not "move".)
      //空操作,保证迭代器接口的完备
      back_insert_iterator&
      operator++()
      { return *this; }

      /// Simply returns *this.  (This %iterator does not "move".)
      //空操作,保证迭代器接口的完备
      back_insert_iterator
      operator++(int)
      { return *this; }
    };

C++17

C++17采用了新的设计(尚无源码,看不出实现的区别)

应用

应用时候一般是使用 back_inserter
back_inserter 的实现源码如下:

  /**
   *  @param  x  A container of arbitrary type.
   *  @return  An instance of back_insert_iterator working on @p x.
   *
   *  This wrapper function helps in creating back_insert_iterator instances.
   *  Typing the name of the %iterator requires knowing the precise full
   *  type of the container, which can be tedious and impedes generic
   *  programming.  Using this function lets you take advantage of automatic
   *  template parameter deduction, making the compiler match the correct
   *  types for you.
  */
  //创建一个临时的 back_insert_iterator 对象,执行之后就返回.
  template<typename _Container>
    inline back_insert_iterator<_Container>
    back_inserter(_Container& __x)
    { return back_insert_iterator<_Container>(__x); }

标签: none



添加新评论