Detailed explanation of c/c + + standard library insertion iterator

Detailed explanation of standard library insertion iterator

Insert iterator function: functions such as copy can't change the size of the container, so sometimes the container is empty before copy. If you don't use insert iterator, you can't use functions such as copy.

For example, the following code is wrong:

list<int> lst{1,2,3,4}; 
  list<int> lst2,lst3;
copy(lst.cbegin(), lst.cend(), lst2.begin());

Lst2 is an empty container. The copy function cannot expand container lst2, so a runtime error will occur.

Using insert iterator can solve the above problems well

list<int> lst{1,2,3,4}; 
  list<int> lst2,lst3;
copy(lst.cbegin(), lst.cend(), inserter(lst3, lst3.begin()));

Three kinds of insertion iterators

iterator Function description
back_inserter Create an iterator using push back
front_inserter Create an iterator using push front
inserter Create an iterator that uses insert before the element is inserted into the specified location

What's special about inserter:

//Suppose it is an iterator generated by an inserter
*it = val;//The effect is the same as the following two lines of code
it = c.insert(it, val);//it points to the newly added element
++it;//Increment it so that it points to the original element

Example:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

int main(){
  //The copy function does not change the size of the container, but with the insert iterator,                        
  //It will change the size of the container                                                        
  /*                                                                            
  list<int> lst{1,2,3,4};                                                       
  list<int> lst2,lst3;                                                          
  //Run error, because lst2 is an empty list, the copy function does not increase the size of the container                       
  //copy(lst.cbegin(), lst.cend(), lst2.begin()); 
  //Results: 4,3,2,1
  copy(lst.cbegin(), lst.cend(), front_inserter(lst2));         
  //Results: 1,2,3,4                
  copy(lst.cbegin(), lst.cend(), inserter(lst3, lst3.begin()));                 
  for(auto const &s : lst2){                                                    
    cout << s << " ";                                                           
  }                                                                             
  cout << endl;                                                                 
  for(auto const &s : lst3){                                                    
    cout << s << " ";                                                           
  }                                                                             
  cout << endl;                                                                 
  */

  //Unique? Copy duplicate elements to a new container                              
  vector<int> ivec{1,2,1,2,3,4,3,3,3,2,2,1,1,1};
  list<int> lst;
  sort(ivec.begin(),ivec.end());
  unique_copy(ivec.cbegin(), ivec.cend(),back_inserter(lst));
  for(auto const &s : lst){
    cout << s << " ";
  }
  cout << endl;

}

QQ group of mutual learning in c/c + +: 877684253

My wechat: xiaoshitou5854

Keywords: C++

Added by bschmitt78 on Sat, 28 Dec 2019 19:08:37 +0200