Open In App

vector :: assign() in C++ STL

Last Updated : 09 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
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 


Previous Article
Next Article

Similar Reads

vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
vector::at() and vector::swap() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::at() at() function is used reference the element present at the position given as the parameter to the function. Syntax: vectorname.at(position) Parameter
4 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin() begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
Initializing Vector using an Existing Vector in C++ STL
A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector&lt;data_structure/type&gt; vector_name(size, item) To know more about vectors refer to vectors in C++. The 3D vector in C
5 min read
How to Assign a std::vector Using a C-Style Array?
In C++, std::vector are the dynamic array containers that replace the plain old C-style arrays and it is very common to assign the values of those C-Style to std::vector. In this article, we will learn how to assign a std::vector using C-style arrays. For Example, Input:arr[] = {1, 5, 7, 9, 8}vector&lt;int&gt; vec = {}Output:vec = {1, 5, 7, 9, 8}As
2 min read
deque assign() function in C++ STL
The deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container. Syntax: deque_name.assign(size, val) Parameters: The functio
2 min read
Article Tags :
Practice Tags :