Syntax:
#include <deque> 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:
val
before loc
, returning an iterator to the element inserted,num
copies of val
before loc
, orstart
to end
before loc
.For example:
// Create a deque, load it with the first 10 characters of the alphabet deque<char> alphaDeque; for( int i=0; i < 10; i++ ) { alphaDeque.push_back( i + 65 ); } // Insert four C's into the deque deque<char>::iterator theIterator = alphaDeque.begin(); alphaDeque.insert( theIterator, 4, 'C' ); // Display the deque for( theIterator = alphaDeque.begin(); theIterator != alphaDeque.end(); theIterator++ ) { cout << *theIterator; }
This code would display:
CCCCABCDEFGHIJ
This next example uses several different methods to add data to a deque, and then uses the copy algorithm to display the deque:
deque<int> dq; dq.push_back(42); dq.push_front(1); dq.insert( dq.begin()+1, 2 ); dq[2] = 16; copy( dq.begin(), dq.end(), ostream_iterator<int>(cout," ") ); // displays "1 2 16"
Related Topics: assign, erase, push_back, push_front