std::basic_string_view<CharT,Traits>::compare

Compares two character sequences.

# Declarations

constexpr int compare( basic_string_view v ) const noexcept;

(since C++17)

constexpr int compare( size_type pos1, size_type count1,
basic_string_view v ) const;

(since C++17)

constexpr int compare( size_type pos1, size_type count1, basic_string_view v,
size_type pos2, size_type count2 ) const;

(since C++17)

constexpr int compare( const CharT* s ) const;

(since C++17)

constexpr int compare( size_type pos1, size_type count1,
const CharT* s ) const;

(since C++17)

constexpr int compare( size_type pos1, size_type count1,
const CharT* s, size_type count2 ) const;

(since C++17)

# Parameters

# Return value

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

# Example

#include <string_view>
 
int main()
{
    using std::operator""sv;
    static_assert("abc"sv.compare("abcd"sv) < 0);
    static_assert("abcd"sv.compare("abc"sv) > 0);
    static_assert("abc"sv.compare("abc"sv) == 0);
    static_assert(""sv.compare(""sv) == 0);
}

# See also