cppreference.com
Std::vector<t,allocator>:: assign.
Replaces the contents of the container.
All iterators, pointers and references to the elements of the container are invalidated. The past-the-end iterator is also invalidated.

[ edit ] Parameters
[ edit ] complexity, [ edit ] example.
The following code uses assign to add several characters to a std:: vector < char > :
[ edit ] See also
- Recent changes
- Offline version
- What links here
- Related changes
- Upload file
- Special pages
- Printable version
- Permanent link
- Page information
- In other languages
- This page was last modified on 4 December 2020, at 13:41.
- This page has been accessed 434,767 times.
- Privacy policy
- About cppreference.com
- Disclaimers

- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
- C++11 <cfenv> (fenv.h)
- <cfloat> (float.h)
- C++11 <cinttypes> (inttypes.h)
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
- C++11 <cstdbool> (stdbool.h)
- <cstddef> (stddef.h)
- C++11 <cstdint> (stdint.h)
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
- C++11 <ctgmath> (tgmath.h)
- <ctime> (time.h)
- C++11 <cuchar> (uchar.h)
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
- C++11 <array>
- <deque>
- C++11 <forward_list>
- <list>
- <map>
- <queue>
- <set>
- <stack>
- C++11 <unordered_map>
- C++11 <unordered_set>
- <vector>
Input/Output:
- <fstream>
- <iomanip>
- <ios>
- <iosfwd>
- <iostream>
- <istream>
- <ostream>
- <sstream>
- <streambuf>
Multi-threading:
- C++11 <atomic>
- C++11 <condition_variable>
- C++11 <future>
- C++11 <mutex>
- C++11 <thread>
- <algorithm>
- <bitset>
- C++11 <chrono>
- C++11 <codecvt>
- <complex>
- <exception>
- <functional>
- C++11 <initializer_list>
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
- C++11 <random>
- C++11 <ratio>
- C++11 <regex>
- <stdexcept>
- <string>
- C++11 <system_error>
- C++11 <tuple>
- C++11 <type_traits>
- C++11 <typeindex>
- <typeinfo>
- <utility>
- <valarray>
- vector<bool>
- vector::~vector
- vector::vector
member functions
- vector::assign
- vector::back
- vector::begin
- vector::capacity
- C++11 vector::cbegin
- C++11 vector::cend
- vector::clear
- C++11 vector::crbegin
- C++11 vector::crend
- C++11 vector::data
- C++11 vector::emplace
- C++11 vector::emplace_back
- vector::empty
- vector::end
- vector::erase
- vector::front
- vector::get_allocator
- vector::insert
- vector::max_size
- vector::operator[]
- vector::operator=
- vector::pop_back
- vector::push_back
- vector::rbegin
- vector::rend
- vector::reserve
- vector::resize
- C++11 vector::shrink_to_fit
- vector::size
- vector::swap
non-member overloads
- relational operators (vector)
- swap (vector)
std:: vector ::assign
Return value, iterator validity, exception safety.
- Standard Template Library
- STL Vectors
- STL Priority Queue
- STL Multiset
- STL Multimap
- STL Unordered Set
- STL Unordered Map
- STL Unordered Multiset
- STL Unordered Multimap
- STL Iterators
- STL Algorithm

- Explore Our Geeks Community
- Vector in C++ STL
- Initialize a vector in C++ (7 different ways)
Commonly Used Methods
- vector::begin() and vector::end() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::front() and vector::back() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector insert() Function in C++ STL
- vector emplace() function in C++ STL
vector :: assign() in C++ STL
- vector erase() and clear() in C++
Other Member Methods
- vector max_size() function in C++ STL
- vector capacity() function in C++ STL
- vector rbegin() and rend() function in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector : : resize() in C++ STL
- vector shrink_to_fit() function in C++ STL
- Using std::vector::reserve whenever possible
- vector data() function in C++ STL
- 2D Vector In C++ With User Defined Size
- Passing Vector to a Function in C++
- How does a vector work in C++?
- How to implement our own Vector Class in C++?
- Advantages of vector over array in C++

Common Vector Programs
- Sorting a vector in C++
- How to reverse a Vector using STL in C++?
- How to find the minimum and maximum element of a Vector using STL in C++?
- How to find index of a given element in a Vector in C++
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:
Program 1: The program below shows how to assign constant values to a vector
The syntax for assigning values from an array or list:
Program 2: The program below shows how to assign values from an array or list
The syntax for modifying values from a vector
Program 3: The program below shows how to modify the vector
Time Complexity – Linear O(N)
Syntax for assigning values with initializer list:
Program 4:The program below shows how to assign a vector with an initializer list.
Please Login to comment...

- poulami21ghosh
- utkarshgupta110092
Please write us at contrib[email protected] to report any issue with the above content
C++ Vector Library - assign() Function
Description.
The C++ function std::vector::assign() assign new values to the vector elements by replacing old ones. It modifies size of vector if necessary.
If memory allocation happens allocation is allocated by internal allocator.
Declaration
Following is the declaration for std::vector::assign() function form std::vector header.
n − Size of vector.
val − Value for each element.
Return value
This member function never throws exception.
Time complexity
Linear i.e. O(n)
The following example shows the usage of std::vector::assign() function.
Let us compile and run the above program, this will produce the following result −
Kickstart Your Career
Get certified by completing the course
vector::assign
If InIt is an integer type, the first member function behaves the same as assign((size_type)first, (T)last) . Otherwise, the first member function replaces the sequence controlled by *this with the sequence [first, last) , which must not overlap the initial controlled sequence. The second member function replaces the sequence controlled by *this with a repetition of n elements of value x .
std::vector<T,Allocator>::assign
Replaces the contents of the container.
All iterators, pointers and references to the elements of the container are invalidated. The past-the-end iterator is also invalidated.
The following code uses assign to add several characters to a std:: vector < char > :
© cppreference.com Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0. https://en.cppreference.com/w/cpp/container/vector/assign
std::vector:: assign
Replaces the contents of the container.
[ edit ] Parameters
[ edit ] complexity, [ edit ] example.
The following code uses assign to add several characters to a std:: vector < char > :
[ edit ] See also
Something went wrong. Wait a moment and try again.

IMAGES
VIDEO
COMMENTS
The vector equation of a line is r = a + tb. Vectors provide a simple way to write down an equation to determine the position vector of any point on a given straight line. In order to write down the vector equation of any straight line, two...
Vectors are used in everyday life to locate individuals and objects. They are also used to describe objects acting under the influence of an external force. A vector is a quantity with a direction and magnitude.
For each vector, the angle of the vector to the horizontal must be determined. Using this angle, the vectors can be split into their horizontal and vertical components using the trigonometric functions sine and cosine.
std::vector<T,Allocator>::assign · 1) Replaces the contents with count copies of value value. · 2) Replaces the contents with copies of those in
Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly. ... In the range version (1), the
vector :: assign() in C++ STL ... vector:: assign() is an STL in C++ which assigns new values to the vector elements by replacing old ones. It can
std::vector< int > numbers; // пустой вектор. numbers.push_back(5); ... langs.assign({ "C++" , "C#" , "C" }); // langs = { "C++", "C#", "C"}. Еще .....
std::vector<T,Allocator>::assign · 1) Заменяет содержимое копиями count со значением value . · 2) Заменяет содержимое копиями из диапазона [first, last) .
The C++ function std::vector::assign() assign new values to the vector elements by replacing old ones. It modifies size of vector if necessary.
The value of {1,3,4 6} may not be predefined values, they would depend on some logic and conditions: if(condition_a) {v[0] = 0; v[1]=3; ...}
vector::assign. template<class InIt> void assign(InIt first, InIt last); void assign(size_type n, const T& x);. If InIt is an integer type, the first member
std::vector<T,Allocator>::assign. (1). void assign( size_type count, const T& value );, (until C++20). constexpr void assign( size_type count, const T& value );
std::vector::assign · 1) Replaces the contents with count copies of value value · 2) Replaces the contents with copies of those in the range [first, last) . · 3
You can assign values to elements of a vector in C++ STL using the square bracket operator ([code ][][/code]) or the [code ]at()[/code]