24 Strings library [strings]

24.3 String classes [string.classes]

24.3.2 Class template basic_­string [basic.string]

24.3.2.7 basic_­string string operations [string.ops]

24.3.2.7.1 basic_­string accessors [string.accessors]

const charT* c_str() const noexcept; const charT* data() const noexcept;
Returns: A pointer p such that p + i == &operator[](i) for each i in [0, size()].
Complexity: Constant time.
Requires: The program shall not alter any of the values stored in the character array.
charT* data() noexcept;
Returns: A pointer p such that p + i == &operator[](i) for each i in [0, size()].
Complexity: Constant time.
Requires: The program shall not alter the value stored at p + size().
operator basic_string_view<charT, traits>() const noexcept;
Effects: Equivalent to: return basic_­string_­view<charT, traits>(data(), size());
allocator_type get_allocator() const noexcept;
Returns: A copy of the Allocator object used to construct the string or, if that allocator has been replaced, a copy of the most recent replacement.

24.3.2.7.2 basic_­string​::​find [string.find]

size_type find(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Effects: Determines the lowest position xpos, if possible, such that both of the following conditions hold:
  • pos <= xpos and xpos + sv.size() <= size();
  • traits​::​eq(at(xpos + I), sv.at(I)) for all elements I of the data referenced by sv.
Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Effects: Equivalent to: return find(basic_­string_­view<charT, traits>(str), pos);
size_type find(const charT* s, size_type pos, size_type n) const;
Returns: find(basic_­string_­view<charT, traits>(s, n), pos).
size_type find(const charT* s, size_type pos = 0) const;
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Returns: find(basic_­string_­view<charT, traits>(s), pos).
size_type find(charT c, size_type pos = 0) const;
Returns: find(basic_­string(1, c), pos).

24.3.2.7.3 basic_­string​::​rfind [string.rfind]

size_type rfind(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Effects: Determines the highest position xpos, if possible, such that both of the following conditions hold:
  • xpos <= pos and xpos + sv.size() <= size();
  • traits​::​eq(at(xpos + I), sv.at(I)) for all elements I of the data referenced by sv.
Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Effects: Equivalent to: return rfind(basic_­string_­view<charT, traits>(str), pos);
size_type rfind(const charT* s, size_type pos, size_type n) const;
Returns: rfind(basic_­string_­view<charT, traits>(s, n), pos).
size_type rfind(const charT* s, size_type pos = npos) const;
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Returns: rfind(basic_­string_­view<charT, traits>(s), pos).
size_type rfind(charT c, size_type pos = npos) const;
Returns: rfind(basic_­string(1, c), pos).

24.3.2.7.4 basic_­string​::​find_­first_­of [string.find.first.of]

size_type find_first_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Effects: Determines the lowest position xpos, if possible, such that both of the following conditions hold:
  • pos <= xpos and xpos < size();
  • traits​::​eq(at(xpos), sv.at(I)) for some element I of the data referenced by sv.
Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Effects: Equivalent to: return find_­first_­of(basic_­string_­view<charT, traits>(str), pos);
size_type find_first_of(const charT* s, size_type pos, size_type n) const;
Returns: find_­first_­of(basic_­string_­view<charT, traits>(s, n), pos).
size_type find_first_of(const charT* s, size_type pos = 0) const;
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Returns: find_­first_­of(basic_­string_­view<charT, traits>(s), pos).
size_type find_first_of(charT c, size_type pos = 0) const;
Returns: find_­first_­of(basic_­string(1, c), pos).

24.3.2.7.5 basic_­string​::​find_­last_­of [string.find.last.of]

size_type find_last_of(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Effects: Determines the highest position xpos, if possible, such that both of the following conditions hold:
  • xpos <= pos and xpos < size();
  • traits​::​eq(at(xpos), sv.at(I)) for some element I of the data referenced by sv.
Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Effects: Equivalent to: return find_­last_­of(basic_­string_­view<charT, traits>(str), pos);
size_type find_last_of(const charT* s, size_type pos, size_type n) const;
Returns: find_­last_­of(basic_­string_­view<charT, traits>(s, n), pos).
size_type find_last_of(const charT* s, size_type pos = npos) const;
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Returns: find_­last_­of(basic_­string_­view<charT, traits>(s), pos).
size_type find_last_of(charT c, size_type pos = npos) const;
Returns: find_­last_­of(basic_­string(1, c), pos).

24.3.2.7.6 basic_­string​::​find_­first_­not_­of [string.find.first.not.of]

size_type find_first_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Effects: Determines the lowest position xpos, if possible, such that both of the following conditions hold:
  • pos <= xpos and xpos < size();
  • traits​::​eq(at(xpos), sv.at(I)) for no element I of the data referenced by sv.
Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Effects: Equivalent to:
return find_first_not_of(basic_string_view<charT, traits>(str), pos);
size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
Returns: find_­first_­not_­of(basic_­string_­view<charT, traits>(s, n), pos).
size_type find_first_not_of(const charT* s, size_type pos = 0) const;
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Returns: find_­first_­not_­of(basic_­string_­view<charT, traits>(s), pos).
size_type find_first_not_of(charT c, size_type pos = 0) const;
Returns: find_­first_­not_­of(basic_­string(1, c), pos).

24.3.2.7.7 basic_­string​::​find_­last_­not_­of [string.find.last.not.of]

size_type find_last_not_of(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Effects: Determines the highest position xpos, if possible, such that both of the following conditions hold:
  • xpos <= pos and xpos < size();
  • traits​::​eq(at(xpos), sv.at(I)) for no element I of the data referenced by sv.
Returns: xpos if the function can determine such a value for xpos.
Otherwise, returns npos.
size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Effects: Equivalent to:
return find_last_not_of(basic_string_view<charT, traits>(str), pos);
size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
Returns: find_­last_­not_­of(basic_­string_­view<charT, traits>(s, n), pos).
size_type find_last_not_of(const charT* s, size_type pos = npos) const;
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Returns: find_­last_­not_­of(basic_­string_­view<charT, traits>(s), pos).
size_type find_last_not_of(charT c, size_type pos = npos) const;
Returns: find_­last_­not_­of(basic_­string(1, c), pos).

24.3.2.7.8 basic_­string​::​substr [string.substr]

basic_string substr(size_type pos = 0, size_type n = npos) const;
Throws: out_­of_­range if pos > size().
Effects: Determines the effective length rlen of the string to copy as the smaller of n and size() - pos.
Returns: basic_­string(data()+pos, rlen).

24.3.2.7.9 basic_­string​::​compare [string.compare]

int compare(basic_string_view<charT, traits> sv) const noexcept;
Effects: Determines the effective length rlen of the strings to compare as the smaller of size() and sv.size().
The function then compares the two strings by calling traits​::​compare(data(), sv.data(), rlen).
Returns: The nonzero result if the result of the comparison is nonzero.
Otherwise, returns a value as indicated in Table 58.
Table 58 — compare() results
Condition
Return Value
size() <  sv.size()
< 0
size() == sv.size()
 0
size() >  sv.size()
> 0
int compare(size_type pos1, size_type n1, basic_string_view<charT, traits> sv) const;
Effects: Equivalent to:
return basic_string_view<charT, traits>(data(), size()).substr(pos1, n1).compare(sv);
template<class T> int compare(size_type pos1, size_type n1, const T& t, size_type pos2, size_type n2 = npos) const;
Effects: Equivalent to:
basic_string_view<charT, traits> sv = t;
return basic_string_view<charT, traits>(
    data(), size()).substr(pos1, n1).compare(sv.substr(pos2, n2));
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.
int compare(const basic_string& str) const noexcept;
Effects: Equivalent to: return compare(basic_­string_­view<charT, traits>(str));
int compare(size_type pos1, size_type n1, const basic_string& str) const;
Effects: Equivalent to: return compare(pos1, n1, basic_­string_­view<charT, traits>(str));
int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos) const;
Effects: Equivalent to:
return compare(pos1, n1, basic_string_view<charT, traits>(str), pos2, n2);
int compare(const charT* s) const;
Returns: compare(basic_­string(s)).
int compare(size_type pos, size_type n1, const charT* s) const;
Returns: basic_­string(*this, pos, n1).compare(basic_­string(s)).
int compare(size_type pos, size_type n1, const charT* s, size_type n2) const;
Returns: basic_­string(*this, pos, n1).compare(basic_­string(s, n2)).