[string.assign]

# 27 Strings library [[strings]](./#strings)

## 27.4 String classes [[string.classes]](string.classes#string.assign)

### 27.4.3 Class template basic_string [[basic.string]](basic.string#string.assign)

#### 27.4.3.7 Modifiers [[string.modifiers]](string.modifiers#string.assign)

#### 27.4.3.7.3 basic_string​::​assign [string.assign]

[ð](#lib:assign,basic_string)

`constexpr basic_string& assign(const basic_string& str);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3432)

*Effects*: Equivalent to: return *this = str;

[ð](#lib:assign,basic_string_)

`constexpr basic_string& assign(basic_string&& str)
  noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
           allocator_traits<Allocator>::is_always_equal::value);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3445)

*Effects*: Equivalent to: return *this = std​::​move(str);

[ð](#lib:assign,basic_string__)

`constexpr basic_string& assign(const basic_string& str, size_type pos, size_type n = npos);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3457)

*Effects*: Equivalent to:return assign(basic_string_view<charT, traits>(str).substr(pos, n));

[ð](#lib:assign,basic_string___)

`template<class T>
  constexpr basic_string& assign(const T& t);
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3472)

*Constraints*: 

- [(4.1)](#4.1)

is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and

- [(4.2)](#4.2)

is_convertible_v<const T&, const charT*> isfalse[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3483)

*Effects*: Equivalent to:basic_string_view<charT, traits> sv = t;return assign(sv.data(), sv.size());

[ð](#lib:assign,basic_string____)

`template<class T>
  constexpr basic_string& assign(const T& t, size_type pos, size_type n = npos);
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3499)

*Constraints*: 

- [(6.1)](#6.1)

is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and

- [(6.2)](#6.2)

is_convertible_v<const T&, const charT*> isfalse[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3510)

*Effects*: Equivalent to:basic_string_view<charT, traits> sv = t;return assign(sv.substr(pos, n));

[ð](#lib:assign,basic_string_____)

`constexpr basic_string& assign(const charT* s, size_type n);
`

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3525)

*Preconditions*: [s, s + n) is a valid range[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3529)

*Effects*: Replaces the string controlled by *this with
a copy of the range [s, s + n)[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3534)

*Returns*: *this[.](#10.sentence-1)

[ð](#lib:assign,basic_string______)

`constexpr basic_string& assign(const charT* s);
`

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3545)

*Effects*: Equivalent to: return assign(s, traits​::​length(s));

[ð](#lib:assign,basic_string_______)

`constexpr basic_string& assign(initializer_list<charT> il);
`

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3556)

*Effects*: Equivalent to: return assign(il.begin(), il.size());

[ð](#lib:assign,basic_string________)

`constexpr basic_string& assign(size_type n, charT c);
`

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3567)

*Effects*: Equivalent to:clear();
resize(n, c);return *this;

[ð](#lib:assign,basic_string_________)

`template<class InputIterator>
  constexpr basic_string& assign(InputIterator first, InputIterator last);
`

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3584)

*Constraints*: InputIterator is a type that qualifies as an input
iterator ([[container.reqmts]](container.reqmts "23.2.2.2 Container requirements"))[.](#14.sentence-1)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3589)

*Effects*: Equivalent to: return assign(basic_string(first, last, get_allocator()));

[ð](#lib:assign_range,basic_string)

`template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<charT> R>
  constexpr basic_string& assign_range(R&& rg);
`

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3601)

*Effects*: Equivalent to: return assign(basic_string(from_range, std​::​forward<R>(rg), get_allocator()));
