Bagikan melalui


inserter

An iterator adaptor that adds a new element to a container at a specified point of insertion.

template<class Container, class Iterator> 
   insert_iterator<Container> inserter( 
      Container& _Cont,  
      Iterator _It 
   );

Parameters

  • _Cont
    The container to which new elements are to be added.

  • _It
    An iterator locating the point of insertion.

Return Value

An insert iterator addressing the new element inserted.

Remarks

Within the Standard Template Library, the sequence and sorted associative containers may be used as a container object _Cont with the templatized inserter.

Example

// iterator_inserter.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;
   int i;
   list <int>::iterator L_Iter;

   list<int> L;
   for (i = 2 ; i < 5 ; ++i )  
   {
      L.push_back ( 10 * i );
   }

   cout << "The list L is:\n ( ";
   for ( L_Iter = L.begin( ) ; L_Iter != L.end( ); L_Iter++ )
      cout << *L_Iter << " ";
   cout << ")." << endl;

   // Using the template version to insert an element
   insert_iterator<list <int> > Iter( L, L.begin ( ) );
   *Iter = 1;

   // Alternatively, using the member function to insert an element
   inserter ( L, L.end ( ) ) = 500;

   cout << "After the insertions, the list L is:\n ( ";
   for ( L_Iter = L.begin( ) ; L_Iter != L.end( ); L_Iter+)
      cout << *L_Iter << " ";
   cout << ")." << endl;
}

The list L is: ( 20 30 40 ). After the insertions, the list L is: ( 1 20 30 40 500 ).

Requirements

Header: <iterator>

Namespace: std

See Also

Reference

Standard Template Library

Other Resources

<iterator> Members