|
template <class InputIterator>
void assign ( InputIterator first, InputIterator last );
void assign ( size_type n, const T& u );
Assign vector content
Assigns new content to the vector object, dropping all the elements contained in the vector before the call and replacing them by those specified by the parameters:
In the first version (with iterators), the new contents are a copy of the elements in the sequence between first and last (in the range [first,last)).
In the second version, the new content is the repetition n times of copies of element u.
Parameters
- first, last
- Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The template type can be any type of input iterator.
- n
- Times that u is repeated to form the new content of the object.
Member type size_type is an unsigned integral type.
- u
- Value to be repeated n times as the new content of the object.
T is the first class template parameter (the type of the elements stored in the vector).
Return value
none
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
// vector assign
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> first;
vector<int> second;
vector<int> third;
first.assign (7,100); // a repetition 7 times of value 100
vector<int>::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
cout << "Size of third: " << int (third.size()) << endl;
return 0;
}
|
Output:
Size of first: 7
Size of second: 5
Size of third: 3
|
Complexity
Linear on initial and final sizes (destruction, copy construction).
In the case of the iteration version, if the iterators are forward, bidirectional or random-access, the new capacity is determined before beginning, otherwise logarithmic complexity (object reallocations) must be added on top of that.
See also
|