Comparison operators
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
比较的参数.
Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
---|---|---|---|---|
Inside class definition | Outside class definition | |||
equal to | a == b
|
Yes | bool T::operator ==(const T2 &b) const; | bool operator ==(const T &a, const T2 &b); |
not equal to | a != b
|
Yes | bool T::operator !=(const T2 &b) const; | bool operator !=(const T &a, const T2 &b); |
less than | a < b
|
Yes | bool T::operator <(const T2 &b) const; | bool operator <(const T &a, const T2 &b); |
greater than | a > b
|
Yes | bool T::operator >(const T2 &b) const; | bool operator >(const T &a, const T2 &b); |
less than or equal to | a <= b
|
Yes | bool T::operator <=(const T2 &b) const; | bool operator <=(const T &a, const T2 &b); |
greater than or equal to | a >= b
|
Yes | bool T::operator >=(const T2 &b) const; | bool operator >=(const T &a, const T2 &b); |
|
目录 |
[编辑] 解释
返回布尔结果的参数,这些参数不被修改的值的比较.
原文:
Returns the boolean result of comparison of the values of the arguments, which are not modified.
[编辑] 算术比较操作符
对于每一个对促进算术类型
L
R
,包括枚举类型,下面的函数签名参与重载决议原文:
For every pair of promoted arithmetic types
L
and R
, including enumeration types, the following function signatures participate in overload resolution: bool operator<(L, R); |
||
bool operator>(L, R); |
||
bool operator<=(L, R); |
||
bool operator>=(L, R); |
||
bool operator==(L, R); |
||
bool operator!=(L, R); |
||
如果操作数进行算术或枚举类型(范围或无作用域),“通常的算术转换”算术运算符的规则。值进行比较后的转换
原文:
If the operands has arithmetic or enumeration type (scoped or unscoped), usual arithmetic conversions are performed following the rules for 算术运算符. The values are compared after conversions:
[编辑] 示例
#include <iostream> int main() { std::cout << std::boolalpha; int n = -1; int n2 = 1; std::cout << " -1 == 1? " << (n == n2) << '\n' << "Comparing two signed values:\n" << " -1 < 1? " << (n < n2) << '\n' << " -1 > 1? " << (n > n2) << '\n'; unsigned int u = 1; std::cout << "Comparing signed and unsigned:\n" << " -1 < 1? " << (n < u) << '\n' << " -1 > 1? " << (n > u) << '\n'; unsigned char uc = 1; std::cout << "Comparing signed and smaller unsigned:\n" << " -1 < 1? " << (n < uc) << '\n' << " -1 > 1? " << (n > uc) << '\n'; }
输出:
-1 == 1? false Comparing two signed values: -1 < 1? true -1 > 1? false Comparing signed and unsigned: -1 < 1? false -1 > 1? true Comparing signed and smaller unsigned: -1 < 1? true -1 > 1? false
[编辑] 指针的比较操作符
P
要么是指向对象或指针到函数或std::nullptr_t每一种类型,每一种类型的MP
这是一个成员对象或成员函数的指针的指针,下面的函数签名参与重载解析原文:
For every type
P
which is either pointer to object or pointer to function or std::nullptr_t, and for every type MP
that is a pointer to member object or pointer to member function, the following function signatures participate in overload resolution: bool operator<(P, P); |
||
bool operator>(P, P); |
||
bool operator<=(P, P); |
||
bool operator>=(P, P); |
||
bool operator==(P, P); |
||
bool operator!=(P, P); |
||
bool operator==(MP, MP); |
||
bool operator!=(MP, MP); |
||
比较操作符可以用来比较两个指针(指针成员,operator==和operator!=只),或一个指针和一个空指针常量或两个空指针常量(但只要其中至少有一个是std::nullptr_t:NULL与NULL的比较如下算术比较规则)。 指针转换(成员转换的指针,如果参数是指向成员的指针)和资格转换适用于两个操作数,以获得“复合指针类型”,如下所示
原文:
Comparison operators can be used to compare two pointers (or pointers-to-members, for operator== and operator!= only), or a pointer and a null pointer constant, or two null pointer constants (but only as long as at least one of them is std::nullptr_t: comparison of NULL and NULL follows arithmetic comparison rules). 指针转换 (pointer to member conversions if the arguments are pointers to members) and 资格转换 are applied to both operands to obtain the composite pointer type, as follows
1)
如果两个操作数都为空指针常量,指针类型的复合std::nullptr_t
原文:
If both operands are null pointer constants, the composite pointer type is std::nullptr_t
2)
一个空指针常量和其他如果一个操作数是一个指针,是完全的复合型指针类型
原文:
If one operand a null pointer constant and the other is a pointer, the composite type is exactly the pointer type
3)
如果两个操作数的指针同类型,同不同的CV资格,复合材料是指向相同类型的CV资格,这是一个工会的简历资格的论据.
原文:
If both operands are pointers to the same type, with different cv-qualification, the composite is pointer to the same type with cv-qualification that is a union of the cv-qualifications of the arguments.
请注意,这意味着任何指针,可与void*.
原文:
Note that this implies that any pointer can be compared with void*.
比较两个指针(转换)后的结果如下决定:1
原文:
Results of comparing two pointers (after conversions) are determined as follows:
1)
a)
指向同一对象或函数
b)
或指向同一数组的一个过去
原文:
or point one past the end of the same array
c)
或都空指针
然后指针“比较平等”:p==q,p<=q,和p>=q回报true,而p!=q,p<q,和p>q回报false
原文:
then the pointers compare equal: p==q, p<=q, and p>=q return true, while p!=q, p<q, and p>q return false,
2)
如果一个操作数是一个空指针,另一种是不,他们比较不平等的:p==q回报true,p!=q回报false,其他运营商的行为是不确定的.....
原文:
If one of the operands is a null pointer and the other is not, they compare unequal: p==q returns true, p!=q returns false, the behavior of other operators is unspecified.
3)
如果指针
p
和q
指向相同的数组成员的a[i]a[j]或一个过去的数组,他们的结果比较指针作为比较指标的结果是一样的:如果i<j==true然后{{{1}}}.原文:
If the pointers
p
and q
point to members of the same array a[i] and a[j] or one past the end of the array, they results of comparing the pointers is the same as the result of comparing the indexes: if i<j==true then {{{1}}}.4)
如果指针
p
和q
点在同一类或不同的基子对象在同一个派生类中,其成员或子对象的非静态数据成员,递归的,如果指向的成员/子对象有相同的访问控制和类(如都public:
),是一个联盟,然后比较大于早先宣布的子对象/成员指针的指针后宣布的子对象/会员。换句话说,类成员的三种接入方式中声明的顺序在内存中的位置.原文:
If the pointers
p
and q
point to non-static data members within the same class or different base subobjects within the same derived class, or to their members or subobjects, recursively, and if the pointed-to members/subobjects have the same access control (e.g. both public:
), and the class is not a union, then the pointer to the later declared subobject/member compares greater than the pointer to the earlier declared subobject/member. In other words, class members in each of the three access modes are positioned in memory in order of declaration.6)
如果指针
p
和q
点相同union的成员,他们比较是相等的(通常是显式转换为void*需要一个操作数)原文:
If the pointers
p
and q
point to members of the same union, they compare equal (typically an explicit conversion to void* is required for one of the operands)7)
如果void和两个指针指向同一个地址指针或空指针的指针之一是,他们比较相等
原文:
If one of the pointers is a pointer to void and both pointers point to the same address or are both null pointers, they compare equal.
8)
如果两个空指针常量相比,他们比较相等
原文:
If two null pointer constants are compared, they compare equal.
9)
如果两个操作数的指针成员(对象或函数),他们比较平等的,如果它们都指向最派生类成员相同.
原文:
If both operands are pointers to member (object or function), they compare equal if they both point to the same member of the most derived class.
10)
否则(如果指针指向不同的数组中的对象或不同的功能,或某些对象的成员具有不同的访问控制等),结果p<q,p>q,p<=q,并p>=q是不确定的,p!=q回报false。
原文:
Otherwise (if the pointers point to objects in different arrays, or to different functions, or to members of some object with different access control, etc), the results of p<q, p>q, p<=q, and p>=q are unspecified, and p!=q returns false.
[编辑] 示例
#include <iostream> struct Foo { int n1; int n2; }; union Union { int n; double d; }; int main() { std::cout << std::boolalpha; char a[4] = "abc"; char* p1 = &a[1]; char* p2 = &a[2]; std::cout << "Pointers to array elements: p1 == p2 " << (p1 == p2) << ", p1 < p2 " << (p1 < p2) << '\n'; Foo f; int* p3 = &f.n1; int* p4 = &f.n2; std::cout << "Pointers to members of a class: p3 == p4 " << (p3 == p4) << ", p3 < p4 " << (p3 < p4) << '\n'; Union u; int* p5 = &u.n; double* p6 = &u.d; std::cout << "Pointers to members of a union: p5 == (void*)p6 " << (p5 == (void*)p6) << ", p5 < p6 " << (p5 < (void*)p6) << '\n'; }
输出:
Pointers to array elements: p1 == p2 false, p1 < p2 true Pointers to members of a class: p3 == p4 false, p3 < p4 true Pointers to members of a union: p5 == (void*)p6 true, p5 < p6 false
[编辑] 注释
因为这些操作员组左到右,表达a<b<c解析(a<b)<c,而不是a<(b<c)或(a<b)&&(b<c).
原文:
Because these operators group left-to-right, the expression a<b<c is parsed (a<b)<c, and not a<(b<c) or (a<b)&&(b<c).
用户定义的一个普遍要求,
operator<
是严格弱序。尤其是,这是必需的标准算法和容器与LessThanComparable
类型:std::sort,std::max_element,std::map等原文:
A common requirement for user-defined
operator<
is 严格弱序. In particular, this is required by the standard algorithms and containers that work with LessThanComparable
types: std::sort, std::max_element, std::map, etc.虽然随机起源的指针(如不都指向同一个数组的成员)比较的结果是不确定的,许多实现提供了严格全序的指针,例如:如果它们实现为连续的虚拟地址空间内的地址。这些实现不(例如,如果不是所有位的指针是一个存储器地址的一部分,并具有用于比较被忽略,或者需要额外的计算或其他指针和不是一个整数是1对1的关系),提供一个专业化的指针std::less,保证。这使得它可以使用标准关联容器,如键的所有指针的随机起源std::set或std::map.
原文:
Although the results of comparing pointers of random origin (e.g. not all pointing to members of the same array) is unspecified, many implementations provide 严格全序 of pointers, e.g. if they are implemented as addresses within continuous virtual address space. Those implementations that do not (e.g. where not all bits of the pointer are part of a memory address and have to be ignored for comparison, or an additional calculation is required or otherwise pointer and integer is not a 1 to 1 relationship), provide a specialization of std::less for pointers that has that guarantee. This makes it possible to use all pointers of random origin as keys in standard associative containers such as std::set or std::map.
本章尚未完成 原因:equivalence vs. equality |
[编辑] 标准库
比较运算符重载,多类标准库中的.
原文:
Comparison operators are overloaded for many classes in the standard library.
checks whether the objects refer to the same type (公共成员函数of std::type_info )
| |
比较2 error_code s(函数) | |
比较error_conditions和error_codes 原文: compares error_conditions and error_codes (函数) | |
根据字典顺序比较pair中的值 (函数模板) | |
根据字典顺序比较元组中的值 (函数模板) | |
的内容进行比较 (公共成员函数of std::bitset )
| |
比较两个分配器的实例 (公共成员函数of std::allocator )
| |
另一个 unique_ptr 比较或与nullptr 原文: compares to another unique_ptr or with nullptr (函数模板) | |
与另一个 shared_ptr 或nullptr 原文: compares with another shared_ptr or with nullptr (函数模板) | |
比较std::functionstd::nullptr 原文: compares an std::function with std::nullptr (函数模板) | |
比较两个持续时间 (函数模板) | |
比较两个时间点 (函数模板) | |
比较两个scoped_allocator_adaptor情况 原文: compares two scoped_allocator_adaptor instances (公共成员函数of std::scoped_allocator_adaptor )
| |
比较基础std::type_info对象 原文: compares the underlying std::type_info objects (公共成员函数of std::type_index )
| |
按字典顺序比较两个字符串 原文: lexicographically compares two strings (函数模板) | |
平等Locale对象之间的比较 原文: equality comparison between locale objects (公共成员函数of std::locale )
| |
根据字典顺序比较的array中的值 原文: lexicographically compares the values in the array (函数模板) | |
根据字典顺序比较的deque中的值 原文: lexicographically compares the values in the deque (函数模板) | |
根据字典顺序比较的forward_list中的值 原文: lexicographically compares the values in the forward_list (函数模板) | |
根据字典顺序比较的list中的值 原文: lexicographically compares the values in the list (函数模板) | |
根据字典顺序比较的vector中的值 原文: lexicographically compares the values in the vector (函数模板) | |
根据字典顺序比较的map中的值 原文: lexicographically compares the values in the map (函数模板) | |
根据字典顺序比较的multimap中的值 原文: lexicographically compares the values in the multimap (函数模板) | |
根据字典顺序比较的set中的值 原文: lexicographically compares the values in the set (函数模板) | |
根据字典顺序比较的multiset中的值 原文: lexicographically compares the values in the multiset (函数模板) | |
比较在unordered_map值 原文: compares the values in the unordered_map (函数模板) | |
比较在unordered_multimap值 原文: compares the values in the unordered_multimap (函数模板) | |
比较在unordered_set值 原文: compares the values in the unordered_set (函数模板) | |
比较在unordered_multiset值 原文: compares the values in the unordered_multiset (函数模板) | |
根据字典顺序比较的queue中的值 原文: lexicographically compares the values in the queue (函数模板) | |
根据字典顺序比较的stack中的值 原文: lexicographically compares the values in the stack (函数模板) | |
比较两个reverse_iterators的平等 原文: compares two reverse_iterators for equality (函数模板) | |
订单reverse_iterators (函数模板) | |
比较2 move_iterator s (函数模板) | |
比较2 istream_iterators (函数模板) | |
比较2 istreambuf_iterators (函数模板) | |
比较两个复数或一个复杂和一个标量 原文: compares two complex numbers or a complex and a scalar (函数模板) | |
比较两个valarrays或valarray的一个值 原文: compares two valarrays or a valarray with a value (函数模板) | |
比较两个伪随机数引擎的内部状态的 原文: compares the internal states of two pseudo-random number engines (函数) | |
比较两个分布对象 (函数) | |
字典在容器中的值进行比较 原文: lexicographically compares the values in the container (函数) | |
字典的值进行比较,在这两个比赛的结果 原文: lexicographically compares the values in the two match result (函数模板) | |
比较2 regex_iterator s (函数模板) | |
比较2 regex_token_iterator s (函数模板) | |
比较两个 thread::id 对象(函数) | |
automatically generates comparison operators based on user-defined operator== and operator< (函数模板) |
[编辑] 另请参阅
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||