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

std::basic_string::compare

性病:基本[医]字符串:比较

int compare( const basic_string& str ) const;(1)
int compare( size_type pos1, size_type count1, const basic_string& str ) const;(2)
(3)
int compare( size_type pos1, size_type count1, const basic_string& str, size_type pos2, size_type count2 ) const;(until C++14)
int compare( size_type pos1, size_type count1, const basic_string& str, size_type pos2, size_type count2 = npos ) const;(since C++14)
int compare( const CharT* s ) const;(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)
int compare( std::basic_string_view<CharT, Traits> sv) const;(7)(since C++17)
int compare( size_type pos1, size_type count1, std::basic_string_view<CharT, Traits> sv) const;(8)(since C++17)
template < class T > int compare( size_type pos1, size_type count1, const T& t, size_type pos2, size_type count2 = npos) const;(9)(since C++17)

比较两个字符序列。

1%29将此字符串与斯塔尔。首先,计算要比较的字符数,好像size_type rlen =std::min(size(), str.size()).然后通过调用比较Traits::compare(data(), str.data(), rlen).对于标准字符串,此函数执行逐个字符的字典比较。如果结果为0%28--到目前为止字符串等于%29,那么它们的大小比较如下:

ConditionResultReturn value
Traits::compare(data, arg, rlen) < 0data is less than arg<0
Traits::compare(data, arg, rlen) == 0size(data) < size(arg)data is less than arg
size(data) == size(arg)data is equal to arg​0​
size(data) > size(arg)data is greater than arg0
Traits::compare(data, arg, rlen) > 0data is greater than arg0

2%29比较a[pos1, pos1+count1)此字符串的子字符串为斯塔尔好像basic_string(*this, pos1, count1).compare(str)%28直到C++17%29compare(pos1, count1,std::basic_string_view<CharT, Traits>(str))%28自C++17%29

3%29比较a[pos1, pos1+count1)将此字符串的子字符串转换为子字符串。[pos2, pos2+count2)成斯塔尔好像basic_string(*this, pos1, count1).compare(basic_string(str, pos2, count2))%28直到C++17%29compare(pos1, count1,std::basic_string_view<CharT, Traits>(str), pos2, count2)%28自C++17%29

4%29将此字符串与以,好像compare(basic_string(s))

5%29比较a[pos1, pos1+count1)将此字符串的子字符串转换为以,好像basic_string(*this, pos, count1).compare(basic_string(s))

6%29比较a[pos1, pos1+count1)此字符串的子字符串到第一个字符串。count2所指向的第一个字符的字符数组中的字符。,好像basic_string(*this, pos, count1).compare(basic_string(s, count2)).%28注:ss+count2可能包含空字符%29%29

7%29将此字符串与sv,与%281%29相似,但使用sv.size()sv.data()而不是str.size()str.data()

8%29比较a[pos1, pos1+count1)此字符串的子字符串为sv,好像std::basic_string_view<CharT, Traits>(data(), size()).substr(pos1, count1).compare(sv)

9%29名皈依者t到字符串视图sv好像std::basic_string_view<CharT, Traits> sv = t;,然后比较[pos1, pos1+count1)将此字符串的子字符串转换为子字符串。[pos2, pos2+count2)成sv好像std::basic_string_view<CharT, Traits>(data(), size()).substr(pos1, count1).compare(sv.substr(pos2, count2)。此重载只参与以下情况下的过载解决方案:std::is_convertible_v<const T&,std::basic_string_view<CharT, Traits>>是true和std::is_convertible_v<const T&, const CharT*>是false...

参数

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
sv-std::basic_string_view to compare to
t-object (convertible to std::basic_string_view) to compare to

返回值

负值*this按字典顺序出现在参数指定的字符序列之前。

如果两个字符序列比较等效,则为零。

正值*this在参数指定的字符序列之后,按字典顺序显示。

例外

1%29

(none)(until C++11)
noexcept specification: noexcept(since C++11)

2-6%5月29日抛出由相应的basic_string建筑工人。

7) noexcept specification: noexcept(since C++17)

可能的实施

模板<类图,类特征,类异种>INTBASIC[医]字符串<图表,性状,异基因>::比较%28 const std::basic[医]String&s%29 Const no,{size除外[医]LHS型[医]SZ=尺寸%28%29;尺寸[医]RHS型[医]SZ=S.size%28%29;INT结果=性状[医]类型:比较%28data%28%29,s.data%28%29,std::min%28 lhs[医]SZ,RHS[医]SZ%29%29;if%28结果%21=0%29返回结果;if%28 lhs[医]SZ<RHS[医]SZ%29返回-1;if%28 lhs[医]SZ>RHS[医]SZ%29返回1;返回0;}

*。

注记

对于不需要进行三方比较的情况,std::basic_string提供通常的关系算子%28<,,,<=,,,==,,,>,等...%29。

默认情况下,%28与默认值std::char_traits%29,此函数不对区域设置敏感.。见std::collate::compare用于区域识别的三向字符串比较。

另见

operator==operator!=operatoroperator<=operator>=lexicographically compares two strings (function template)
substrreturns a substring (public member function)
collatedefines lexicographical comparison and hashing of strings (class template)
strcollcompares two strings in accordance to the current locale (function)
lexicographical_comparereturns true if one range is lexicographically less than another (function template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/string/basic[医]字符串/比较