std::basic_string<CharT,Traits,Allocator>::compare (3) - Linux Manuals
std::basic_string<CharT,Traits,Allocator>::compare: std::basic_string<CharT,Traits,Allocator>::compare
NAME
std::basic_string<CharT,Traits,Allocator>::compare - std::basic_string<CharT,Traits,Allocator>::compare
Synopsis
int compare( const basic_string& str ) const; (until C++11)
int compare( const basic_string& str ) const noexcept; (since C++11)
int compare( size_type pos1, size_type count1, (2)
const basic_string& str ) const;
int compare( size_type pos1, size_type count1,
const basic_string& str, (until C++14)
size_type pos2, size_type count2 ) const;
int compare( size_type pos1, size_type count1,
const basic_string& str, (since C++14)
size_type pos2, size_type count2 = npos ) const;
int compare( const CharT* s ) const; (4)
int compare( size_type pos1, size_type count1, (1) (5)
const CharT* s ) const;
int compare( size_type pos1, size_type count1, (3) (6)
const CharT* s, size_type count2 ) const;
template < class T > (7) (since C++17)
int compare( const T& t ) const noexcept(/* see below */);
template < class T >
int compare( size_type pos1, size_type count1, (8) (since C++17)
const T& t ) const;
template < class T >
int compare( size_type pos1, size_type count1, (9) (since C++17)
const T& t,
size_type pos2, size_type count2 = npos) const;
Compares two character sequences.
1) Compares this string to str.
2) Compares a [pos1, pos1+count1) substring of this string to str. If count1 > size() - pos1 the substring is [pos1, size()).
3) Compares a [pos1, pos1+count1) substring of this string to a substring [pos2, pos2+count2) of str. If count1 > size() - pos1 the first substring is [pos1, size()). Likewise, count2 > str.size() - pos2 the second substring is [pos2, str.size()).
4) Compares this string to the null-terminated character sequence beginning at the character pointed to by s with length Traits::length(s).
5) Compares a [pos1, pos1+count1) substring of this string to the null-terminated character sequence beginning at the character pointed to by s with length Traits::length(s) If count1 > size() - pos1 the substring is [pos1, size()).
6) Compares a [pos1, pos1+count1) substring of this string to the characters in the range [s, s + count2). If count1 > size() - pos1 the substring is [pos1, size()). (Note: the characters in the range [s, s + count2) may include null characters.)
7) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares this string to sv. This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
8) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [pos1, pos1+count1) substring of this string to sv, as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
9) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [pos1, pos1+count1) substring of this string to a substring [pos2, pos2+count2) of sv as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv.substr(pos2, count2)). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
A character sequence consisting of count1 characters starting at data1 is compared to a character sequence consisting of count2 characters starting at data2 as follows. First, calculate the number of characters to compare, as if by size_type rlen = std::min(count1, count2). Then compare the sequences by calling Traits::compare(data1, data2, rlen). For standard strings this function performs character-by-character lexicographical comparison. If the result is zero (the character sequences are equal so far), then their sizes are compared as follows:
Condition Result Return value
Traits::compare(data1, data2, rlen) < 0 data1 is less than data2 <0
Traits::compare(data1, data2, rlen) == 0 size1 == size2 data1 is equal to data2 0
Traits::compare(data1, data2, rlen) > 0 data1 is greater than data2 >0
Parameters
str - other string to compare to
s - pointer to the character string to compare to
count1 - number of characters of this string to compare
pos1 - position of the first character in this string to compare
count2 - number of characters of the given string to compare
pos2 - position of the first character of the given string to compare
t - object (convertible to std::basic_string_view) to compare to
Return value
negative value if *this appears before the character sequence specified by the arguments, in lexicographical order
zero if both character sequences compare equivalent
positive value if *this appears after the character sequence specified by the arguments, in lexicographical order
Exceptions
The overloads taking parameters named pos1 or pos2 throws std::out_of_range if the argument is out of range.
7)
noexcept specification:
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
8-9) Throws anything thrown by the conversion to basic_string_view.
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_2946 C++17 string_view overload causes ambiguity in some cases avoided by making it a template
Possible implementation
Notes
For the situations when three-way comparison is not required, std::basic_string provides the usual relational_operators (<, <=, ==, >, etc).
By default (with the default std::char_traits), this function is not locale-sensitive. See std::collate::compare for locale-aware three-way string comparison.
Example
// Run this code