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.
Syntax for assigning constant values:
vectorname.assign(int size, int value) Parameters:
Program 1: The program below shows how to assign constant values to a vector
// 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
Syntax for assigning values from an array or list:
vectorname.assign(arr, arr + size) Parameters:
Program 2: The program below shows how to assign values from an array or list
// 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
Syntax for modifying values from a vector
vectorname.assign(InputIterator first, InputIterator last) Parameters:
Program 3: The program below shows how to modify the vector
// 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- vector::crend() & vector::crbegin() with example
- vector::empty() and vector::size() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::front() and vector::back() in C++ STL
- std::string::assign() in C++
- forward_list assign() function in C++ STL
- deque assign() function in C++ STL
- list assign() function in C++ STL
- C++ 17 | New ways to Assign values to Variables
- Conditionally assign a value without using conditional and arithmetic operators
- Programming puzzle (Assign value without any control statement)
- vector::at() and vector::swap() in C++ STL
- vector::begin() and vector::end() in C++ STL
- Vector in C++ STL
- Deque vs Vector in C++ STL
- How to reverse a Vector using STL in C++?
- Modifiers for Vector in C++ STL
- How does a vector work in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

