Open In App

Vector assign() in C++ STL

Last Updated : 22 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the vector assign() is a built-in method used to assign the new values to the given vector by replacing old ones. It also modifies the size of the vector according to the given number of elements.

 Let’s take a look at an example that shows the how to use this function.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v;

    // Assign a vector of size 3 with value 9
    v.assign(3, 9);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
9 9 9 

Explanation: The vector assign() function resize the vector to 3 and initialized with specific value 9.

This article covers the syntax, usage, and common examples of vector assign() method in C++:

Syntax of Vector assign()

The vector assign() is the member method of std::vector class defined inside <vector> header file and have 3 implementations:

v.assign(n, val); // Assigning the single value

v.assign({val1, val2, val3,...}); // Assigning the multiple value

v.assign(first, last); // Assigning from another container

Parameters

  • n: New size of the vector container.
  • val: Specific value by which we initialize the n elements of vector.
  • val1, val2, val3, ...: List of values to assign.
  • first: Iterator to the first element in the range.
  • last: Iterator to the theoretical element just after the last element in the range.

Return Value

  • This function does not return any value.

Examples of vector assign()

The following examples demonstrate the use of vector assign() function for different purposes:

Resize Vector and Assign a Default Value

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v;

    // Assign vector of size 7 with value 6
    v.assign(7, 6);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
6 6 6 6 6 6 6 

Explanation: The vector assign() method to change the size of the vector and fill it with a single repeated value.

Assign New Values to Vector from Initializer List

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v({5, 7, 8, 6, 9});

    // Assining values using initializer list
    v.assign({1, 2, 3});

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 

Explanation: The vector assign() can also assign multiple different values to the vector container by using the initializer list. The new size will be the number of elements in the initializer list.

Assign Values to Vector from Another Container

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1;
    vector<int> v2 = {5, 6, 8, 3};
    int arr[] = {1, 2, 3};

    // Assign first 2 values from array in v1
    v1.assign(arr, arr + 2);

    // Assign all the values from v1 to v2
    v2.assign(v1.begin(), v1.end());

    for (auto i : v1)
        cout << i << " ";
    cout << endl;
    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
1 2 
1 2 

The vector assign() method can also be used to assign multiple values in the vector container from the given range. This range can be any STL container like vector, set, etc or an array.


Next Article
Article Tags :
Practice Tags :

Similar Reads