std::vector<T,Allocator>::assign

Replaces the contents of the container.

# Declarations

void assign( size_type count, const T& value );

(constexpr since C++20)

template< class InputIt >
void assign( InputIt first, InputIt last );

(constexpr since C++20)

void assign( std::initializer_list<T> ilist );

(since C++11) (constexpr since C++20)

# Parameters

# Example

#include <vector>
#include <iostream>
#include <string>
 
int main()
{
    std::vector<char> characters;
 
    auto print_vector = [&]()
    {
        for (char c : characters)
            std::cout << c << ' ';
        std::cout << '\n';
    };
 
    characters.assign(5, 'a');
    print_vector();
 
    const std::string extra(6, 'b');
    characters.assign(extra.begin(), extra.end());
    print_vector();
 
    characters.assign({'C', '+', '+', '1', '1'});
    print_vector();
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2209C++98the replacement operation was required to be implemented aserasing all existing elements followed by inserting the given elementsremoved therequirement

# See also