STL iterator adapter (iterator adapter)

iterator adapter

graph LR iterator --- reverse_iterator iterator --- Insert_iterator iterator --- iostream_iterator Insert_iterator --- back_insert_iterator Insert_iterator --- front_insert_iterator Insert_iterator --- insert_iterator

Insert iterator: the assignment change is generally iterator insertion operation.

insert iterator

When the user inserter iterator for the assignment, it calls the underlying container operator () inserting operation (push_back, push_front, insert, etc.) Defined in the inserter iterator class.

In iterative adapter insert back_inserter Example: (front_inserter and inserter similar to that class)

template <class _Container>
inline back_insert_iterator<_Container> back_inserter(_Container& __x) {
  return back_insert_iterator<_Container>(__x);
}

When the user invokes back_inserter function, the function returns the actual insertion iterator class (back_insert_iterator) objects, and the container after the parameters passed to the constructor to insert iterator class. After insertion iterator class code (back_insert_iterator):

template <class _Container>
class back_insert_iterator {
protected:
  _Container* container;
public:
  typedef _Container          container_type;
  typedef output_iterator_tag iterator_category;
  typedef void                value_type;
  typedef void                difference_type;
  typedef void                pointer;
  typedef void                reference;

  explicit back_insert_iterator(_Container& __x) : container(&__x) {}
  back_insert_iterator<_Container>&
  operator=(const typename _Container::value_type& __value) { 
    container->push_back(__value);
    return *this;
  }
  back_insert_iterator<_Container>& operator*() { return *this; }
  back_insert_iterator<_Container>& operator++() { return *this; }
  back_insert_iterator<_Container>& operator++(int) { return *this; }
};

Container class class pointer _Container * container, the container to be inserted point. Defined in the class assignment operator =, the function call push_back bottom of the container, the operator = parameter __value inserted end of the container.

Class iterator is overloaded operators: dereference [*], prefix_increment [],post_increment[(Int)]. In the body of the function which simply returns the current iteration itself (* this), not the corresponding operations such as forward, such back_insert_iterator object described above is the operation will not have any effect.

Analysis with examples:

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>

using namespace std;

int main(){
	int ia[]={1,5,3,6,4,8,9};
	vector<int> ivec(ia,ia+7);
	ostream_iterator<int> outite(cout," ");

	copy(ia,ia+3,back_inserter(ivec));        //a
	copy(ivec.begin(),ivec.end(),outite);
        //1 5 3 6 4 8 9 1 5 3
	cout<<endl;

	return 0;
}

 Back_inserter a program line calls function, to generate the vector after ivec iterator object (back_insert_iterator object), and passing the object to the copy algorithm. With this object in the copy algorithm as a function of the objects in the container for processing. In the program, after just being used as a function of the iterator object (functor) used to call the object function provides for container, rather than the traditional iterator to access elements in the array, therefore, be dereference [ *], prefix_increment [],post_increment[(Int)] operation will not produce any results.

Running result of the program is:. 3. 5. 6. 4. 8. 1. 9 . 1. 5. 3

Guess you like

Origin www.cnblogs.com/SupremeGIS-Developer/p/11964352.html