insert [C++ Reference]
Keywords: stl,list,insert
Date: 2010-04-20T09:19:53-0700

## insert

Syntax:

        #include <list>
        iterator insert( iterator loc, const TYPE& val );
        void insert( iterator loc, size_type num, const TYPE& val );
        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 list, load it with the first 10 characters of the alphabet
       list<char> alphaList;
       for( int i=0; i < 10; i++ ) {
         static const char letters[] = "ABCDEFGHIJ";
         alphaList.push_back( letters[i] );
       }

       // Insert four C's into the list
       list<char>::iterator theIterator = alphaList.begin();
       alphaList.insert( theIterator, 4, 'C' );

       // Display the list
       for( theIterator = alphaList.begin(); theIterator != alphaList.end();
      ++theIterator )    {
         cout << *theIterator;
       }

This code would display:

       CCCCABCDEFGHIJ

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