C++ Vector assign() Function

Last Updated : 7 Apr 2026

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.

Syntax

It has the following syntax:

In this syntax,

  • (first,last): It defines the range.first, which is an input iterator pointing to the first element, and last is an input iterator referring to the past the last element.
  • n: It represents the number of times the value occurs.
  • val: It defines the value that is to be assigned.

Return value

It does not return any value.

Examples of vector assign() function in C++

Here, we are going to take several examples to demonstrate the working of the vector assign() function in C++.

Example 1: Assigning a Range of Elements from One Vector to Another

Let us take an example to demonstrate how to assign a range of elements from one vector to another using the vector assign() function.

Compile and Run

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.

Example 2: Assigning Multiple Copies of a Value to a Vector

The following example demonstrates how we can assign multiple copies of a value to a vector using the vector assign() function.

Compile and Run

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.

Example 3: Reassigning Values to an Existing Vector

Let's take an example to demonstrate how to reassign values to an existing vector in C++.

Compile and Run

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.

Example 4: Using different uses of the Vector Assign() Function

The following example demonstrates the different uses of the vector assign() Function in C++.

Compile and Run

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.


Next TopicC++ Vector