Scroll to navigation

std::basic_string::assign(3) C++ Standard Libary std::basic_string::assign(3)

NAME

std::basic_string::assign - std::basic_string::assign

Synopsis

basic_string& assign( const basic_string& str ); (1) (constexpr since
C++20) basic_string& assign( basic_string&& str ) (since C++11) noexcept(/* see below */); (2) (constexpr since
C++20) basic_string& assign( size_type count, CharT ch ); (3) (constexpr since
C++20) basic_string& assign( const CharT* s, size_type (4) (constexpr since count ); C++20) basic_string& assign( const CharT* s ); (5) (constexpr since
C++20) template< class SV > (since C++17) basic_string& assign( const SV& t ); (6) (constexpr since
C++20) template< class SV >
(since C++17) basic_string& assign( const SV& t, (7) (constexpr since
C++20) size_type pos, size_type count = npos); basic_string& assign( const basic_string& str, (until C++14) size_type pos, size_type count ); basic_string& assign( const basic_string& str, (since C++14) size_type pos, size_type count = npos); (constexpr since
(8) C++20) template< class InputIt > (9) (constexpr since basic_string& assign( InputIt first, InputIt last ); C++20) basic_string& assign( std::initializer_list<CharT> (since C++11) ilist ); (10) (constexpr since
C++20)

Replaces the contents of the string.

1) Equivalent to return *this = str;. 2) Equivalent to return *this = std::move(str);. 3) Replaces the contents with count copies of character ch. Equivalent to clear(); resize(n, c); return *this;. 4) Replaces the contents with copies of the characters in the range [s, s + count). If [s, s + count) is not a valid range, the behavior is undefined. 5) Equivalent to return assign(s, Traits::length(s));. 6,7) Replaces the contents with characters in a string view sv constructed from t.
* If only t is provided, replaces the contents with all characters in sv.
* If pos is also provided:
* If count is npos, replaces the contents with all characters in sv starting from
pos.
* Otherwise, replaces the contents with the std::min(count, sv.size() - pos)
characters in sv starting from pos. These overloads participate in overload resolution only if all following conditions are satisfied:
* std::is_convertible_v<const SV&, std::basic_string_view<CharT, Traits>> is true.
* std::is_convertible_v<const SV&, const CharT*> is false. 6) Equivalent to std::basic_string_view<CharT, Traits> sv = t; return assign(sv.data(), sv.size());. 7) Equivalent to std::basic_string_view<CharT, Traits> sv = t; return assign(sv.substr(pos, count));. 8) Replaces the contents with characters in str.
* If count is npos, replaces the contents with all characters in str starting from pos.
* Otherwise, replaces the contents with the std::min(count, str.size() - pos) characters
in str starting from pos.

Equivalent to return assign(std::basic_string_view<CharT, Traits> (since C++20) (str).substr(pos, count));.

9) Equivalent to return assign(basic_string(first, last, get_allocator()));.

This overload participates in overload resolution only if InputIt satisfies (since C++11) the requirements of LegacyInputIterator.

10) Equivalent to return assign(ilist.begin(), ilist.size());.

Parameters

str - string to be used as source to initialize the characters with count - size of the resulting string ch - value to initialize characters of the string with s - pointer to a character string to use as source to initialize the string with t - object (convertible to std::basic_string_view) to initialize the characters
of the string with pos - index of the first character to take first, last - range to copy the characters from ilist - std::initializer_list to initialize the characters of the string with

Return value

*this

Exceptions

2) noexcept specification: noexcept(std::allocator_traits<Allocator>::

propagate_on_container_move_assignment::value ||

std::allocator_traits<Allocator>::is_always_equal::value)

If the operation would cause size() to exceed max_size(), throws std::length_error.

7) If pos > sv.size() is true, throws std::out_of_range. 8) If pos > str.size() is true, throws std::out_of_range.

If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

Example

// Run this code

#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" }

Output:

==== Exemplary == Exemplary Exemplar C++ by example

C-style

C-style

C-style string

C-style


Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.


DR Applied to Behavior as published Correct behavior LWG 847 C++98 there was no exception safety added strong exception safety
guarantee guarantee
non-normative note stated that corrected to require move LWG 2063 C++11 overload (2) assignment
can be implemented by swapping LWG 2250 C++98 the behavior of overload (8) was always throws an exception in
undefined if pos > str.size() is true this case LWG 2579 C++98 overload (1) and the copy assignment they have the same effect
operator had different effects LWG 2946 C++17 overload (6) caused ambiguity in some avoided by making it a template
cases

See also

assign_range assign a range of characters to a string (C++23) (public member function) constructor constructs a basic_string
(public member function) operator= assigns values to the string
(public member function)

2026.11.10 http://cppreference.com