stl:list:insert

C++ Reference

insert

Syntax:

    #include <list>
    iterator insert( iterator loc, const TYPE& val );
    void insert( iterator loc, size_type num, const TYPE& val );
    template<TYPE> void insert( iterator loc, input_iterator start, input_iterator end );

The insert() function either:

  • inserts val before loc, returning an iterator to the element inserted,
  • inserts num copies of val before loc, or
  • inserts the elements from start to end before loc.

For example:

   // Create a vector, load it with the first 10 characters of the alphabet
   vector<char> alphaVector;
   for( int i=0; i < 10; i++ ) {
     alphaVector.push_back( i + 65 );
   }
 
   // Insert four C's into the vector
   vector<char>::iterator theIterator = alphaVector.begin();
   alphaVector.insert( theIterator, 4, 'C' );
 
   // Display the vector
   for( theIterator = alphaVector.begin(); theIterator != alphaVector.end();
  theIterator++ )    {
     cout << *theIterator;
   }

This code would display:

   CCCCABCDEFGHIJ

Related Topics: assign, erase, merge, push_back, push_front, splice