20 Strings library [strings]

20.3 String classes [string.classes]

20.3.2 Class template basic_­string [basic.string]

20.3.2.2 Constructors and assignment operators [string.cons]

explicit basic_string(const Allocator& a) noexcept;
Effects: Constructs an object of class basic_­string.
Ensures: size() is 0 and capacity() is an unspecified value.
basic_string(const basic_string& str); basic_string(basic_string&& str) noexcept;
Effects: Constructs an object of class basic_­string.
Ensures: data() points at the first element of an allocated copy of the array whose first element is pointed at by the original value str.data(), size() is equal to the original value of str.size(), and capacity() is a value at least as large as size().
In the second form, str is left in a valid state with an unspecified value.
basic_string(const basic_string& str, size_type pos, const Allocator& a = Allocator()); basic_string(const basic_string& str, size_type pos, size_type n, const Allocator& a = Allocator());
Throws: out_­of_­range if pos > str.size().
Effects: Constructs an object of class basic_­string and determines the effective length rlen of the initial string value as str.size() - pos in the first form and as the smaller of str.size() - pos and n in the second form.
Ensures: data() points at the first element of an allocated copy of rlen consecutive elements of the string controlled by str beginning at position pos, size() is equal to rlen, and capacity() is a value at least as large as size().
template<class T> basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator());
Effects: Creates a variable, sv, as if by basic_­string_­view<charT, traits> sv = t; and then behaves the same as:
basic_string(sv.substr(pos, n), a);
Remarks: This constructor shall not participate in overload resolution unless is_­convertible_­v<const T&, basic_­string_­view<charT, traits>> is true.
template<class T> explicit basic_string(const T& t, const Allocator& a = Allocator());
Effects: Creates a variable, sv, as if by basic_­string_­view<charT, traits> sv = t; and then behaves the same as basic_­string(sv.data(), sv.size(), a).
Remarks: This constructor shall not participate in overload resolution unless
  • is_­convertible_­v<const T&, basic_­string_­view<charT, traits>> is true and
  • is_­convertible_­v<const T&, const charT*> is false.
basic_string(const charT* s, size_type n, const Allocator& a = Allocator());
Requires: s points to an array of at least n elements of charT.
Effects: Constructs an object of class basic_­string and determines its initial string value from the array of charT of length n whose first element is designated by s.
Ensures: data() points at the first element of an allocated copy of the array whose first element is pointed at by s, size() is equal to n, and capacity() is a value at least as large as size().
basic_string(const charT* s, const Allocator& a = Allocator());
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Effects: Constructs an object of class basic_­string and determines its initial string value from the array of charT of length traits​::​length(s) whose first element is designated by s.
Ensures: data() points at the first element of an allocated copy of the array whose first element is pointed at by s, size() is equal to traits​::​length(s), and capacity() is a value at least as large as size().
Remarks: Shall not participate in overload resolution if Allocator is a type that does not qualify as an allocator ([container.requirements.general]).
[ Note
:
This affects class template argument deduction.
— end note
 ]
basic_string(size_type n, charT c, const Allocator& a = Allocator());
Requires: n < npos.
Effects: Constructs an object of class basic_­string and determines its initial string value by repeating the char-like object c for all n elements.
Ensures: data() points at the first element of an allocated array of n elements, each storing the initial value c, size() is equal to n, and capacity() is a value at least as large as size().
Remarks: Shall not participate in overload resolution if Allocator is a type that does not qualify as an allocator ([container.requirements.general]).
[ Note
:
This affects class template argument deduction.
— end note
 ]
template<class InputIterator> basic_string(InputIterator begin, InputIterator end, const Allocator& a = Allocator());
Effects: If InputIterator is an integral type, equivalent to:
basic_string(static_cast<size_type>(begin), static_cast<value_type>(end), a);
Otherwise constructs a string from the values in the range [begin, end), as indicated in the Sequence Requirements table (see [sequence.reqmts]).
basic_string(initializer_list<charT> il, const Allocator& a = Allocator());
Effects: Same as basic_­string(il.begin(), il.end(), a).
basic_string(const basic_string& str, const Allocator& alloc); basic_string(basic_string&& str, const Allocator& alloc);
Effects: Constructs an object of class basic_­string.
The stored allocator is constructed from alloc.
Ensures: data() points at the first element of an allocated copy of the array whose first element is pointed at by the original value of str.data(), size() is equal to the original value of str.size(), capacity() is a value at least as large as size(), and get_­allocator() is equal to alloc.
In the second form, str is left in a valid state with an unspecified value.
Throws: The second form throws nothing if alloc == str.get_­allocator().
template<class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>> basic_string(InputIterator, InputIterator, Allocator = Allocator()) -> basic_string<typename iterator_traits<InputIterator>::value_type, char_traits<typename iterator_traits<InputIterator>::value_type>, Allocator>;
Remarks: Shall not participate in overload resolution if InputIterator is a type that does not qualify as an input iterator, or if Allocator is a type that does not qualify as an allocator ([container.requirements.general]).
template<class charT, class traits, class Allocator = allocator<charT>> explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator()) -> basic_string<charT, traits, Allocator>; template<class charT, class traits, class Allocator = allocator<charT>> basic_string(basic_string_view<charT, traits>, typename see below::size_type, typename see below::size_type, const Allocator& = Allocator()) -> basic_string<charT, traits, Allocator>;
Remarks: Shall not participate in overload resolution if Allocator is a type that does not qualify as an allocator ([container.requirements.general]).
basic_string& operator=(const basic_string& str);
Returns: *this.
Ensures: If *this and str are the same object, the member has no effect.
Otherwise, data() points at the first element of an allocated copy of the array whose first element is pointed at by str.data(), size() is equal to str.size(), and capacity() is a value at least as large as size().
basic_string& operator=(basic_string&& str) noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value);
Effects: Move assigns as a sequence container, except that iterators, pointers and references may be invalidated.
Returns: *this.
template<class T> basic_string& operator=(const T& t);
Effects: Equivalent to:
{
  basic_string_view<charT, traits> sv = t;
  return assign(sv);
}
Remarks: This function shall not participate in overload resolution unless is_­convertible_­v<const T&, basic_­string_­view<charT, traits>> is true and is_­convertible_­v<const T&, const charT*> is false.
basic_string& operator=(const charT* s);
Returns: *this = basic_­string(s).
Remarks: Uses traits​::​length().
basic_string& operator=(charT c);
Returns: *this = basic_­string(1, c).
basic_string& operator=(initializer_list<charT> il);
Effects: As if by: *this = basic_­string(il);
Returns: *this.