std::basic_string::compare
来自cppreference.com
< cpp | string | basic string
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
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, |
(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) | |
比较两个字符的序列.
1) “海峡”这个字符串进行比较。首先,要比较的字符数计算,如果由size_type rlen = std::min(size(), str.size())。然后通过调用Traits::compare(data(), str.data(), rlen)比较。执行该功能对于标准的字符串字符的字符逐一比较。如果结果是零(字符串相等至今的),那么它们的尺寸对比如下:
原文:
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 |
“”如果海峡
3) [pos1, pos1+count1)
比较basic_string(*this, pos1, count1).compare(str)子字符串,该字符串原文:
Compares a
[pos1, pos1+count1)
substring of this string to str as if by basic_string(*this, pos1, count1).compare(str)比较
4) [pos1, pos1+count1)
子字符串,该字符串的子字符串[pos2, pas2+count2)
“海峡”如果basic_string(*this, pos1, count1).compare(basic_string(str, pos2, count2))原文:
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))比较当前字符串以NULL结尾的字符序列开始的“指向的字符,仿佛compare(basic_string(s))
5) 原文:
Compares this string to the null-terminated character sequence beginning at the character pointed to by s, as if by compare(basic_string(s))
比较
6) [pos1, pos1+count1)
子字符串,该字符串以NULL结尾的字符序列开始的“指向的字符,仿佛basic_string(*this, pos, count1).compare(basic_string(s))原文:
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))比较
[pos1, pos1+count1)
子字符串,该字符串count2
的第一个字符的字符数组的第一个字符指出的“,仿佛basic_string(*this, pos, count1).compare(basic_string(s, count2)).(注:从s
s+count2
的字符可能包含空字符))原文:
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))目录 |
[编辑] 参数
str | - | 其他字符串比较
|
s | - | 比较字符串的指针
原文: pointer to the character string to compare to |
count1 | - | 数量的字符这个字符串compore的
原文: number of characters of this string to compore |
pos1 | - | 在此字符串中第一个字符的位置比较
原文: position of the first character in this string to compare |
count2 | - | 到compore给定的字符串的字符数
原文: number of characters of the given string to compore |
pos2 | - | 比较给定的字符串的第一个字符的位置
原文: position of the first character of the given string to compare |
[编辑] 返回值
负的值,如果此字符串是“小于零”比其他的字符序列,如果两个字符序列是平等的,积极的值,如果此字符串是'更大的比其他的字符序列“.....
原文:
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.
[编辑] 例外
1) 2-6)可能会引发
basic_string
相应的构造函数抛出的异常.原文:
May throw the exceptions thrown by the corresponding
basic_string
constructors.[编辑] 可能的实现
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; } |
[编辑] 示例
本章尚未完成 原因:暂无示例 |
[编辑] 另请参阅
按字典顺序比较两个字符串 原文: lexicographically compares two strings (函数模板) | |
返回一个子字符串 (公共成员函数) | |
定义逐一比较和散列字符串 原文: defines lexicographical comparison and hashing of strings (类模板) | |
按照目前的语言环境比较两个字符串 原文: compares two strings in accordance to the current locale (函数) |