vector:: assign() is an STL in C++ which assigns new values to the vector elements by replacing old ones. It can also modify the size of the vector if necessary.
The syntax for assigning constant values:
vectorname.assign(int size, int value) Parameters: size - number of values to be assigned value - value to be assigned to the vectorname
Program 1: The program below shows how to assign constant values to a vector
// CPP program to demonstrate // how to assign constant values to a vector #include <bits/stdc++.h> using namespace std;
int main()
{ vector< int > v;
v.assign(7, 100);
cout << "Size of first: "
<< int (v.size()) << '\n' ;
cout << "Elements are\n" ;
for ( int i = 0; i < v.size(); i++)
cout << v[i] << endl;
return 0;
} |
Size of first: 7 Elements are 100 100 100 100 100 100 100
The syntax for assigning values from an array or list:
vectorname.assign(arr, arr + size) Parameters: arr - the array which is to be assigned to a vector size - number of elements from the beginning which has to be assigned.
Program 2: The program below shows how to assign values from an array or list
// CPP program to demonstrate // how to assign values to a vector // from a list #include <bits/stdc++.h> using namespace std;
int main()
{ vector< int > v1;
int a[] = { 1, 2, 3 };
// assign first 2 values
v1.assign(a, a + 2);
cout << "Elements of vector1 are\n" ;
for ( int i = 0; i < v1.size(); i++)
cout << v1[i] << " " ;
vector< int > v2;
// assign first 3 values
v2.assign(a, a + 3);
cout << "\nElements of vector2 are\n" ;
for ( int i = 0; i < v2.size(); i++)
cout << v2[i] << " " ;
return 0;
} |
Elements of vector1 are 1 2 Elements of vector2 are 1 2 3
The syntax for modifying values from a vector
vectorname.assign(InputIterator first, InputIterator last) Parameters: first - Input iterator to the initial position range. last - Input iterator to the final position range.
Program 3: The program below shows how to modify the vector
// CPP program to demonstrate // how to modify vector size #include <bits/stdc++.h> using namespace std;
int main()
{ vector< int > v;
v.assign(7, 100);
cout << "Size of first: " << int (v.size()) << '\n' ;
cout << "Elements are\n" ;
for ( int i = 0; i < v.size(); i++)
cout << v[i] << endl;
// modify the elements
v.assign(v.begin(), v.begin() + 3);
cout << "\nModified VectorElements are\n" ;
for ( int i = 0; i < v.size(); i++)
cout << v[i] << endl;
return 0;
} |
Size of first: 7 Elements are 100 100 100 100 100 100 100 Modified VectorElements are 100 100 100
Time Complexity – Linear O(N)
Syntax for assigning values with initializer list:
vectorname.assign((initializer_list) Parameter: initializer_list
Program 4:The program below shows how to assign a vector with an initializer list.
#include <iostream> #include <vector> using namespace std;
int main()
{ vector< int > v;
// Initialize v with an initialization list
v.assign({ 1, 2, 3 });
cout << "The list is:" << endl;
for ( auto i = v.begin(); i != v.end(); i++)
{
// Printing 1 2 3 as output
cout << *i << " " ;
}
return 0;
} |
The list is: 1 2 3