The Wayback Machine - https://web.archive.org/web/20240118180006/https://www.geeksforgeeks.org/vector-assign-in-c-stl/
Open In App
Related Articles

vector :: assign() in C++ STL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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




// 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;
}


Output

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




// 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;
}


Output

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




// 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;
}


Output

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.

C++




#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;
}


Output

The list is:
1 2 3 

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 09 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads