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) | |
“海峡”这个字符串进行比较。首先,要比较的字符数计算,如果由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)子字符串,该字符串比较
4) [pos1, pos1+count1)
子字符串,该字符串的子字符串[pos2, pas2+count2)
“海峡”如果basic_string(*this, pos1, count1).compare(basic_string(str, pos2, count2))比较当前字符串以NULL结尾的字符序列开始的“指向的字符,仿佛compare(basic_string(s))
5) 比较
6) [pos1, pos1+count1)
子字符串,该字符串以NULL结尾的字符序列开始的“指向的字符,仿佛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 | - | |
count1 | - | |
pos1 | - | |
count2 | - | |
pos2 | - |
[编辑] 返回值
负的值,如果此字符串是“小于零”比其他的字符序列,如果两个字符序列是平等的,积极的值,如果此字符串是'更大的比其他的字符序列“.....
[编辑] 例外
1)noexcept 规定:
2-6) noexcept
可能会引发
basic_string
相应的构造函数抛出的异常.[编辑] 可能的实现
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; } |
[编辑] 示例
本节未完成 原因:暂无示例 |
[编辑] 另请参阅
按字典顺序比较两个字符串 (函数模板) | |
(公开成员函数) | |
定义逐一比较和散列字符串 (类模板) | |
按照目前的语言环境比较两个字符串 (函数) |