std::basic_string::compare

From Cppreference

Jump to: navigation, search
int compare( const basic_string& str ) const;
(1)
int compare( size_type pos1, size_type count1,
             const basic_string& str ) const;
(2)
int compare( size_type pos1, size_type count1,

             const basic_string& str,

             size_type pos2, size_type count2 ) const;
(3)
int compare( const CharT* s ) const noexcept;
(4)
int compare( size_type pos1, size_type count1,
             const CharT* s ) const;
(5)
int compare( size_type pos1, size_type count1,
             const CharT* s, size_type count2 ) const;
(6)

Compares two character sequences.

The first one (data) is a substring of the current string, the second one (arg) is a substring of a supplied character sequence. If sizes of the sequences do not match, the shorter sequence is considered being less than the longer sequence. If the sizes (size) of the sequences match, character values are compared. The comparison is done by calling Traits::compare(data, arg, size). For default strings this function does the comparison lexicographically.

Condition Result Return value
size(data) < size(arg) data is less than arg <0
size(data) == size(arg) Traits::compare(data, arg, size) < 0 data is less than arg <0
Traits::compare(data, arg, size) == 0 data is equal to arg 0
Traits::compare(data, arg, size) > 0 data is greater than arg >0
size(data) > size(arg) data is greater than arg >0

1) Compares this string to str (data = *this, arg = str).

2) Compares a [pos1, pos1+count1) substring of this string to str (data = this->substr(pos1, count1), arg = str).

3) Compares a [pos1, pos1+count1) substring of this string to a substring [pos2, pas2+count2) of str (data = this->substr(pos1, count1), arg = str.substr(pos2, count2)}}).

4) Compares this string to the null-terminated character string pointed to by s (data = *this, arg = basic_string(s)).

5) Compares a [pos1, pos1+count1) substring of this string to the null-terminated character string pointed to by s (data = this->substr(pos1, count1), arg = basic_string(s)).

6) Compares a [pos1, pos1+count1) substring of this string to the first count2 characters of the character string pointed to by s (data = this->substr(pos1, count1), arg = basic_string(s, count2)). s can contain null characters.

Contents

[edit] Parameters

str - other string to compare to
s - pointer to the character string to compare to
count1 - number of characters of this string to compore
pos1 - position of the first character in this string to compare
count2 - number of characters of the given string to compore
pos2 - position of the first character of the given string to compare

[edit] Return value

negative value if this string is less than the other character sequence, zero if the both character sequences are equal, positive value if this string is greater than the other character sequence.

[edit] Example

[edit] See also

substr
returns a substring
(public member function)