std::basic_string::compare

From cppreference.com
 
 
 
std::basic_string
Member functions
basic_string::basic_string
basic_string::operator=
basic_string::assign
basic_string::get_allocator
Element access
basic_string::at
basic_string::operator[]
basic_string::front (C++11)
basic_string::back (C++11)
basic_string::data
basic_string::c_str
Iterators
basic_string::begin
basic_string::cbegin

(C++11)
basic_string::end
basic_string::cend

(C++11)
basic_string::rbegin
basic_string::crbegin

(C++11)
basic_string::rend
basic_string::crend

(C++11)
Capacity
basic_string::empty
basic_string::size
basic_string::length
basic_string::max_size
basic_string::reserve
basic_string::capacity
basic_string::shrink_to_fit (C++11)
Operations
basic_string::clear
basic_string::insert
basic_string::erase
basic_string::push_back
basic_string::pop_back (C++11)
basic_string::append
basic_string::operator+=
basic_string::compare
basic_string::replace
basic_string::substr
basic_string::copy
basic_string::resize
basic_string::swap
Search
basic_string::find
basic_string::rfind
basic_string::find_first_of
basic_string::find_first_not_of
basic_string::find_last_of
basic_string::find_last_not_of
Constants
basic_string::npos
Non-member functions
operator+
operator==
operator!=
operator<
operator>
operator<=
operator>=
swap(std::basic_string)
operator<<
operator>>
getline
stoi
stol
stoll
(C++11)
(C++11)
(C++11)
stoul
stoull
(C++11)
(C++11)
stof
stod
stold
(C++11)
(C++11)
(C++11)
to_string (C++11)
to_wstring (C++11)
Helper classes
hash<std::string>
hash<std::wstring>
hash<std::u32string>
hash<std::u16string>
(C++11)
 
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.

1) Compares this string to str. First, calculates the number of characters to compare, as if by size_type rlen = std::min(size(), str.size()). Then compares by calling Traits::compare(data(), str.data(), rlen). For standard strings this function performs character-by-character lexicographical comparison. If the result is zero (the strings are equal so far), then their sizes are compared as follows:

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

2) Compares a [pos1, pos1+count1) substring of this string to str as if by basic_string(*this, pos1, count1).compare(str)

3) Compares a [pos1, pos1+count1) substring of this string to a substring [pos2, pas2+count2) of str as if by basic_string(*this, pos1, count1).compare(basic_string(str, pos2, count2))

4) Compares this string to the null-terminated character sequence beginning at the character pointed to by s, as if by compare(basic_string(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, as if by basic_string(*this, pos, count1).compare(basic_string(s))

6) Compares a [pos1, pos1+count1) substring of this string to the first count2 characters of the character array whose first character is pointed to by s, as if by basic_string(*this, pos, count1).compare(basic_string(s, count2)). (Note: the characters from s to s+count2 may include 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 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

[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] Exceptions

1)
noexcept specification:  
noexcept
  (since C++11)

2-6) May throw the exceptions thrown by the corresponding basic_string constructors.

[edit] Possible implementation

template<class CharT, class Traits, class Alloc>
int basic_string<CharT, Traits, Alloc>::compare(const std::basic_string& str) const noexcept
{
    size_type lhs_sz = size();
    size_type rhs_sz = str.size();
    int result = traits_type::compare(data(), str.data(), std::min(lhs_sz, rhs_sz));
    if (result != 0)
        return result;
    if (lhs_sz < rhs_sz)
        return -1;
    if (lhs_sz > rhs_sz)
        return 1;
    return 0;
}

[edit] Example

[edit] See also

lexicographically compares two strings
(function template)
returns a substring
(public member function)
defines lexicographical comparison and hashing of strings
(class template)
compares two strings in accordance to the current locale
(function)
returns true if one range is lexicographically less than another
(function template)