Skip to content
Related Articles

Related Articles

Improve Article

vector :: assign() in C++ STL

  • Difficulty Level : Easy
  • Last Updated : 29 Oct, 2020

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

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 
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :