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
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
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
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.
- vector::push_back() and vector::pop_back() in C++ STL
- vector::empty() and vector::size() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::front() and vector::back() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::crend() & vector::crbegin() with example
- std::string::assign() in C++
- deque assign() function in C++ STL
- list assign() function in C++ STL
- forward_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::begin() and vector::end() in C++ STL
- vector::at() and vector::swap() in C++ STL
- Vector in C++ STL
- Using std::vector::reserve whenever possible
- Default value of Vector in C++ STL
- How does a vector work in C++?
- How to reverse a Vector using STL 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 email@example.com. 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.