std::basic_string<CharT,Traits,Allocator>::assign

Replaces the contents of the string.

# Declarations

basic_string& assign( const basic_string& str );

(constexpr since C++20)

basic_string& assign( basic_string&& str ) noexcept(/* see below */);

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

basic_string& assign( size_type count, CharT ch );

(constexpr since C++20)

basic_string& assign( const CharT* s, size_type count );

(constexpr since C++20)

basic_string& assign( const CharT* s );

(constexpr since C++20)

template< class SV >
basic_string& assign( const SV& t );

(since C++17) (constexpr since C++20)

template< class SV >
basic_string& assign( const SV& t,
size_type pos, size_type count = npos);

(since C++17) (constexpr since C++20)

basic_string& assign( const basic_string& str,
size_type pos, size_type count );

(until C++14)

basic_string& assign( const basic_string& str,
size_type pos, size_type count = npos);

(since C++14) (constexpr since C++20)

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

(constexpr since C++20)

basic_string& assign( std::initializer_list<CharT> ilist );

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

# Parameters

# Return value

*this

# Example

#include <iostream>
#include <iterator>
#include <string>
 
int main()
{
    std::string s;
    // assign(size_type count, CharT ch)
    s.assign(4, '=');
    std::cout << s << '\n'; // "===="
 
    std::string const c("Exemplary");
    // assign(const basic_string& str)
    s.assign(c);
    std::cout << c << " == " << s << '\n'; // "Exemplary == Exemplary"
 
    // assign(const basic_string& str, size_type pos, size_type count)
    s.assign(c, 0, c.length() - 1);
    std::cout << s << '\n'; // "Exemplar";
 
    // assign(basic_string&& str)
    s.assign(std::string("C++ by ") + "example");
    std::cout << s << '\n'; // "C++ by example"
 
    // assign(const CharT* s, size_type count)
    s.assign("C-style string", 7);
    std::cout << s << '\n'; // "C-style"
 
    // assign(const CharT* s)
    s.assign("C-style\0string");
    std::cout << s << '\n'; // "C-style"
 
    char mutable_c_str[] = "C-style string";
    // assign(InputIt first, InputIt last)
    s.assign(std::begin(mutable_c_str), std::end(mutable_c_str) - 1);
    std::cout << s << '\n'; // "C-style string"
 
    // assign(std::initializer_list<CharT> ilist)
    s.assign({'C', '-', 's', 't', 'y', 'l', 'e'});
    std::cout << s << '\n'; // "C-style"
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee
LWG 2063C++11non-normative note stated that overload (2)can be implemented by swappingcorrected to require move assignment
LWG 2250C++98the behavior of overload (8) wasundefined if pos > str.size() is truealways throws an exception in this case
LWG 2579C++98overload (1) and the copy assignmentoperator had different effectsthey have the same effect
LWG 2946C++17overload (6) caused ambiguity in some casesavoided by making it a template

# See also