21 Strings library [strings]

21.4 Class template basic_string [basic.string]

21.4.7 basic_string string operations [string.ops]

21.4.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.

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.

21.4.7.2 basic_string::find [string::find]

size_type find(const basic_string& str, size_type pos = 0) const noexcept;

Effects: Determines the lowest position xpos, if possible, such that both of the following conditions obtain:

  • pos <= xpos and xpos + str.size() <= size();

  • traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.

Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.

Remarks: Uses traits::eq().

size_type find(const charT* s, size_type pos, size_type n) const;

Returns: find(basic_string(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(s), pos).

size_type find(charT c, size_type pos = 0) const;

Returns: find(basic_string(1,c), pos).

21.4.7.3 basic_string::rfind [string::rfind]

size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;

Effects: Determines the highest position xpos, if possible, such that both of the following conditions obtain:

  • xpos <= pos and xpos + str.size() <= size();

  • traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.

Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.

Remarks: Uses traits::eq().

size_type rfind(const charT* s, size_type pos, size_type n) const;

Returns: rfind(basic_string(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(s), pos).

size_type rfind(charT c, size_type pos = npos) const;

Returns: rfind(basic_string(1,c),pos).

21.4.7.4 basic_string::find_first_of [string::find.first.of]

size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;

Effects: Determines the lowest position xpos, if possible, such that both of the following conditions obtain:

  • pos <= xpos and xpos < size();

  • traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.

Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.

Remarks: Uses traits::eq().

size_type find_first_of(const charT* s, size_type pos, size_type n) const;

Returns: find_first_of(basic_string(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(s), pos).

size_type find_first_of(charT c, size_type pos = 0) const;

Returns: find_first_of(basic_string(1,c), pos).

21.4.7.5 basic_string::find_last_of [string::find.last.of]

size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;

Effects: Determines the highest position xpos, if possible, such that both of the following conditions obtain:

  • xpos <= pos and xpos < size();

  • traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.

Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.

Remarks: Uses traits::eq().

size_type find_last_of(const charT* s, size_type pos, size_type n) const;

Returns: find_last_of(basic_string(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(s), pos).

size_type find_last_of(charT c, size_type pos = npos) const;

Returns: find_last_of(basic_string(1,c),pos).

21.4.7.6 basic_string::find_first_not_of [string::find.first.not.of]

size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;

Effects: Determines the lowest position xpos, if possible, such that both of the following conditions obtain:

  • pos <= xpos and xpos < size();

  • traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str.

Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.

Remarks: Uses traits::eq().

size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;

Returns: find_first_not_of(basic_string(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(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).

21.4.7.7 basic_string::find_last_not_of [string::find.last.not.of]

size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;

Effects: Determines the highest position xpos, if possible, such that both of the following conditions obtain:

  • xpos <= pos and xpos < size();

  • traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str.

Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.

Remarks: Uses traits::eq().

size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;

Returns: find_last_not_of(basic_string(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(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).

21.4.7.8 basic_string::substr [string::substr]

basic_string substr(size_type pos = 0, size_type n = npos) const;

Requires: pos <= size()

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).

21.4.7.9 basic_string::compare [string::compare]

int compare(const basic_string& str) const noexcept;

Effects: Determines the effective length rlen of the strings to compare as the smallest of size() and str.size(). The function then compares the two strings by calling traits::compare(data(), str.data(), rlen).

Returns: The nonzero result if the result of the comparison is nonzero. Otherwise, returns a value as indicated in Table [tab:strings.compare].

Table 72compare() results
ConditionReturn Value
size() < str.size() < 0
size() == str.size() 0
size() > str.size() > 0

int compare(size_type pos1, size_type n1, const basic_string& str) const;

Returns: basic_string(*this,pos1,n1).compare(str).

int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos) const;

Returns: basic_string(*this, pos1, n1).compare(basic_string(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)).