In the C++ programming language, the assign() function is a part of the Standard Template Library (STL) vector container. It is commonly used to assign new values to the vector and replace the old values. With the help of the assign() function, we can easily insert the new value in the place of the old. The assign() function is very helpful when we need to reinitialize a vector with new data elements without making a new vector object.
It has the following syntax:
In this syntax,
It does not return any value.
Here, we are going to take several examples to demonstrate the working of the vector assign() function in C++.
Let us take an example to demonstrate how to assign a range of elements from one vector to another using the vector assign() function.
Output:
2 3 4
Explanation:
In the given example, we illustrate how to use the vector::assign() function to copy a specific range of elements from one vector to another. Here, we use the v1.assign(v.begin()+1, v.end()-1)statement that copies elements starting from the second element up to the second-last element of vector v into vector v1. Finally, the vector v1 stores the values 2, 3, and 4, which are then displayed on the screen using a loop.
The following example demonstrates how we can assign multiple copies of a value to a vector using the vector assign() function.
Output:
T T T T T
Explanation:
In the given example, we have to demonstrate how to use the vector::assign() function to fill a vector with multiple copies of the same value. After that, we use the v.assign(5, 'T') statement that helps to assign five elements with the character 'T' to the vector v, which replaces any existing elements. Finally, the loop iterates through the vector and displays each element on a new line.
Let's take an example to demonstrate how to reassign values to an existing vector in C++.
Output:
100 100 100
Explanation:
In the given example, we demonstrate that using the assign() function, we can remove all existing elements of the vector and replace them with three elements that contain the value 100.
The following example demonstrates the different uses of the vector assign() Function in C++.
Output:
The Vector after assigning 4 copies of value 5: 15 15 15 15 15 15 15 15 15 15 15 15 15 15 The vector after assigning all elements from numbers vector: 10 15 20 25 30 35 The vector after assigning a range from numbers vector: 20 25 30
Explanation:
In the given example, we use the newVec.assign(14, 15) statement that fills the vector newVec with 14 elements having the value 15. Next, we use the newVec.assign(numbers.begin(), numbers.end())statement to replace those elements by copying all values from the numbers vector. Finally, we use the newVec.assign(numbers.begin()+2, numbers.end()-1) statement that assigns a specific range of elements (20, 25, 30) from the original vector. Each call to assign()removes the previous elements and inserts new ones, effectively reinitializing the vector.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India