Syntax:
#include <deque> void assign( size_type num, const TYPE& val ); void assign( input_iterator start, input_iterator end );
The assign() function either gives the current deque the values from start to end, or gives it num copies of val.
This function will destroy the previous contents of the deque.
For example, the following code uses assign() to put 10 copies of the integer 42 into a deque:
deque<int> dq; dq.assign( 10, 42 ); for( deque<int>::size_type i = 0; i < dq.size(); i++ ) { cout << dq[i] << " "; } cout << endl;
The above code displays the following output:
42 42 42 42 42 42 42 42 42 42
The next example shows how assign() can be used to copy one deque to another:
deque<int> dq1; for( int i = 0; i < 10; i++ ) { dq1.push_back( i ); } deque<int> dq2; dq2.assign( dq1.begin(), dq1.end() ); for( deque<int>::size_type i = 0; i < dq2.size(); i++ ) { cout << dq2[i] << " "; } cout << endl;
When run, the above code displays the following output:
0 1 2 3 4 5 6 7 8 9
Related Topics: insert, push_back, push_front