在线文档教程
C++
字符串 | Strings

std::basic_string_view::compare

性病:基本[医]弦[医]意见::比较

constexpr int compare(basic_string_view v) const;(1)(since C++17)
constexpr int compare(size_type pos1, size_type count1, basic_string_view v) const;(2)(since C++17)
constexpr int compare(size_type pos1, size_type count1, basic_string_view v, size_type pos2, size_type count2) const;(3)(since C++17)
constexpr int compare(const CharT* s) const;(4)(since C++17)
constexpr int compare(size_type pos1, size_type count1, const CharT* s) const;(5)(since C++17)
constexpr int compare(size_type pos1, size_type count1, const CharT* s, size_type count2) const;(6)(since C++17)

比较两个字符序列。

1%29长度rlen要比较的序列中,较小的size()v.size()函数通过调用traits::compare(data(), v.data(), rlen),并根据下表返回一个值:

ConditionResultReturn value
Traits::compare(data(), v.data(), rlen) < 0this is less than v<0
Traits::compare(data(), v.data(), rlen) == 0size() < v.size()this is less than v
size() == v.size()this is equal to v​0​
size() > v.size()this is greater than v0
Traits::compare(data(), v.data(), rlen) > 0this is greater than v0

2%29相当于substr(pos1, count1).compare(v)...

3%29相当于substr(pos1, count1).compare(v.substr(pos2, count2))...

4%29相当于compare(basic_string_view(s))...

5%29相当于substr(pos1, count1).compare(basic_string_view(s))...

6%29相当于substr(pos1, count1).compare(basic_string_view(s, count2))...

参数

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

返回值

如果此视图小于其他字符序列,则为负值;如果两个字符序列相等,则为零;如果此视图大于其他字符序列,则为正值。

例外

1%29

noexcept规格:

noexcept

复杂性

1%~29线性在字符数比较。

另见

operator==operator!=operatoroperator<=operator>=lexicographically compares two string views (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/string/basic[医]弦[医]视图/比较