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

Table 63compare() results
ConditionReturn 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)).