assign
Syntax:
#include <list> void assign( size_type num, const TYPE& val ); void assign( input_iterator start, input_iterator end ); The assign() function either gives the current list the values from start to end, or gives it num copies of val. This function will destroy the previous contents of the list. For example, the following code uses assign() to put 10 copies of the integer 42 into a vector: vector<int> v; v.assign( 10, 42 ); for( int i = 0; i < v.size(); i++ ) { cout << v[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 vector to another: vector<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back( i ); } vector<int> v2; v2.assign( v1.begin(), v1.end() ); for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl; When run, the above code displays the following output: 0 1 2 3 4 5 6 7 8 9 |
#include <list> TYPE& back(); const TYPE& back() const;
The back() function returns a reference to the last element in the list.
For example:
vector<int> v; for( int i = 0; i < 5; i++ ) { v.push_back(i); } cout << "The first element is " << v.front() << " and the last element is " << v.back() << endl;
This code produces the following output:
The first element is 0 and the last element is 4
The back() function runs in constant time.