vector :: assign() in C++ STL
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
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
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
Size of first: 7 Elements are 100 100 100 100 100 100 100 Modified VectorElements are 100 100 100
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.
The list is: 1 2 3