From cppreference.com
constexprintcompare(basic_string_viewv)constnoexcept; (1) (since C++17)constexprintcompare(size_typepos1,size_typecount1,basic_string_viewv)const; (2) (since C++17)constexprintcompare(size_typepos1,size_typecount1,basic_string_viewv,size_typepos2,size_typecount2)const; (3) (since C++17)constexprintcompare(constCharT*s)const; (4) (since C++17)constexprintcompare(size_typepos1,size_typecount1,constCharT*s)const; (5) (since C++17)constexprintcompare(size_typepos1,size_typecount1,constCharT*s,size_typecount2)const; (6) (since C++17)Compares two character sequences.
1) The length rlen of the sequences to compare is the smaller of size() and v.size(). The function compares the two views by calling traits::compare(data(),v.data(),rlen), and returns a value according to the following table:
Condition Result Return value Traits::compare(data(), v.data(), rlen) < 0this is less than v<0Traits::compare(data(), v.data(), rlen) == 0size() < v.size()this is less than v<0size() == v.size()this is equal to v0size() > v.size()this is greater than v>0Traits::compare(data(), v.data(), rlen) > 0this is greater than v>02) Equivalent to substr(pos1,count1).compare(v).
3) Equivalent to substr(pos1,count1).compare(v.substr(pos2,count2)).
4) Equivalent to compare(basic_string_view(s)).
5) Equivalent to substr(pos1,count1).compare(basic_string_view(s)).
6) Equivalent to substr(pos1,count1).compare(basic_string_view(s,count2)).
Parameters
v - view to compare s - pointer to the character string to compare to count1 - number of characters of this view to compare pos1 - position of the first character in this view to compare count2 - number of characters of the given view to compare pos2 - position of the first character of the given view to compare 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.
Complexity
1) Linear in the number of characters compared.
Example
Run this code
#include<string_view>intmain(){usingstd::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