首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Comparison operators

比较参数。

Operator name

Syntax

Over?load?able

Prototype examples (for class T)

As member function

As free (namespace) function

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);

所有内置操作符都返回bool,大多数用户定义的重载也返回bool,这样用户定义的操作符就可以与内置操作一样使用。但是,在用户定义的操作符重载中,任何类型都可以用作返回类型%28,包括无效%29。T2可以是任何类型,包括T

  • 所有内置操作符返回bool,而且大多数用户定义的重载还回来bool这样,用户定义的操作符就可以与内置操作一样使用。但是,在用户定义的操作符重载中,任何类型都可以用作返回类型%28,包括void29%。
  • T2可以是任何类型,包括T

解释

返回参数值比较的布尔结果,这些参数未被修改。

算术比较算子

对于每一对提升的算术类型LR,包括枚举类型,下列函数签名参与过载解析:

bool operator<(L, R);

?

?

bool operator>(L, R);

?

?

bool operator<=(L, R);

?

?

bool operator>=(L, R);

?

?

bool operator==(L, R);

?

?

bool operator!=(L, R);

?

?

如果操作数具有算术或枚举类型%28范围或非作用域%29,常用算术变换的规则执行。算术算子.换算后的数值比较:

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
-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%28在C++14%29之前,下列函数签名参与过载解析:

bool operator<(P, P);

?

?

bool operator>(P, P);

?

?

bool operator<=(P, P);

?

?

bool operator>=(P, P);

?

?

bool operator==(P, P);

?

?

bool operator!=(P, P);

?

?

每种类型MP,即指向成员对象的指针或指向成员函数的指针或std::nullptr_t,下列函数签名参与过载解析:

bool operator==(MP, MP);

?

?

bool operator!=(MP, MP);

?

?

比较运算符可用于比较两个指针%28或指向成员的指针。operator==operator!=只有%29,或自C++14%29以来指向成员%28的指针和空指针常数,或两个空指针常数%28,但前提是它们中至少有一个是std::nullptr_tNULL和NULL的比较遵循算术比较规则%29%28,直到C++14%29。

首先,指针转换如果参数是指向成员的指针%29、函数指针转换、%28自C++17%29和资格转换应用于两个操作数以获得复合指针类型,如下。

1%29如果两个操作数都为空指针常量,则复合指针类型为std::nullptr_t

2%29如果一个操作数是空指针常量,而另一个操作数是指针,那么复合类型就是指针类型。

3%29如果一个操作数是指向CV1void另一个是指向CV2T为某种类型T,在哪里T是对象类型或void,复合类型为“指针”。cv 12void“,在哪里cv 12是…的结合CV1CV2

4) 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.

(until C++14)

4) If the types of the operands are P1, a pointer to (possibly cv-qualified) T1, and P2, a pointer to (possibly cv-qualified) T2, and if T1 is the same as T2 or is a base class of T2, then the composite pointer type is the cv-combined type of P1 and P2. Otherwise, if T2 is a base class of T1, then the composite pointer type is the cv-combined type of P2 and P1. 5) If the types of the operands are MP1, pointer to member of T1 of type (possibly cv-qualified) U1 and MP2, pointer to member of T2 of type (possibly cv-qualified) U2, and if T1 is the same as or derived from T2, then the composite pointer type is the cv-combined type of MP1 and MP2. Otherwise, if T2 is derived from T1, then the composite pointer type is the cv-combined type of MP2 and MP1. 6) if the types of the operands P1 and P2 are multi-level mixed pointer and pointer to member types with the same number of levels that only differ by cv-qualifications at any of the levels, the composite pointer type is the cv-combined type of P1 and P2 In the definition above, cv-combined type of two pointer types P1 and P2 is a type P3 that has the same number of levels and type at every level as P1, except that cv-qualifications at every level are set as follows: a) at every level other than top level, the union of the cv-qualifications of P1 and P2 at that level b) if the resulting cv-qualification at any level is different from P1's or P2's cv-qualification at the same level, then const is added to every level between the top level and this one. For example, the composite pointer type of void* and const int* is const void*. The composite pointer type of int** and const int** is const int* const*. Note that until C++14, int** and const int** could not be compared.

(since C++14)

In addition to the above, the composite pointer type between pointer to function and pointer to noexcept function (as long as the function type is the same) is pointer to function.

(since C++17)

请注意,这意味着任何指针都可以与void*...

在转换%29之后,将两个指针与对象%28进行比较的结果定义如下:

1%29如果两个指针指向同一数组的不同元素,或者指向同一数组的不同元素中的次对象,则指向具有较高下标的元素的指针。比较大换句话说,它们比较指针的结果与比较它们所指向的元素的索引的结果是相同的。

2%29如果一个指针指向数组的一个元素,或者指向数组元素的子对象,而另一个指针指向数组的最后一个元素,则后一个指针指向数组的最后一个元素。比较大.指向单个对象的指针被视为指向一个对象数组的指针:&obj+1比较大于&obj%28自C++17%29

3%29如果在非联合类类型的对象中,两个指针指向具有相同属性的不同非静态数据成员。成员访问,或者递归地指向这些成员的次对象或数组元素,指向后一个声明成员的指针。比较大换句话说,三个成员访问模式中的每个类成员都按声明顺序放置在内存中。

在转换%29之后,两个指针%28的相等比较结果定义如下:

1%29如果指针都是空指针值,则它们比较相等

2%29如果指针是指向函数的指针并指向同一个函数,那么比较相等

3%29如果指针是指向对象的指针,并且表示相同的地址,则它们比较相等%28这包括两个指向同一联合的非静态成员的指针、指向标准布局结构的指针以及与重新解释相关的第一个成员的指针。[医]铸造等...29

4%29所有其他指针比较不相等

在转换%29之后,将两个指针与成员%28进行比较的结果定义如下:

1%29如果指向成员的两个指针都为空成员指针值,则它们比较相等...

2%29否则,如果指向成员的两个指针中只有一个是空成员指针值,则它们比较不相等。

3%29否则,如果两者都是指向虚拟成员函数的指针,则结果未指定。

4%29否则,两个指向成员的指针比较相等的当且仅当它们引用同一个派生对象的同一个成员或同一个子对象时,如果它们被关联类类型的假想对象取消引用的话。

5%,29,否则他们就会比较不平等。

如果指针p比较相等指向指针q,,,p<=qp>=q双产率truep<qp>q双产率false...

如果指针p比较大比指针q,然后p>=q,,,p>q,,,q<=p,和q<p全产量truep<=q,,,p<q,,,q>=p,和q>p全产量false...

如果未指定两个指针来比较更大的指针或比较相等的指针,则未指定比较结果。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
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)...

共同要求用户定义运算符<是严格弱序尤其是,这是标准算法和容器所要求的Compare类型:std::sort,,,std::max_element,,,std::map

虽然比较的结果,指针的随机起源%28等。并非所有指向同一数组%29的成员都未指定,许多实现提供严格全序的指针,例如,如果它们是作为连续虚拟地址空间中的地址实现的。那些没有%28例如的实现。如果指针的并非所有位位都是内存地址的一部分,因此必须忽略以进行比较,或者需要额外的计算,否则指针和整数不是1到1的关系%29,则提供一个专门化的std::less有这个保证的指针。这样就可以在标准关联容器中使用所有随机来源的指针作为键,例如std::setstd::map...

对于这两种类型EqualityComparableLessThanComparable,C++标准库对平等,它是表达式的值。a == b等价物,它是表达式的值。!(a < b) && !(b < a)...

在C++14中删除指针和空指针常量之间的比较。

二次

代码语言:javascript
复制
void f(char * p)
{
  if (p > 0) { ... } // OK in C++98..C++11, does not compile in C++14
  if (p > nullptr) { ... } // OK in C++98..C++11, does not compile in C++14
}

二次

标准库

标准库中的许多类都重载了比较运算符。

operator==operator!=

checks whether the objects refer to the same type (public member function of std::type_info)

operator==operator!=operator<

compares two error_codes (function)

operator==operator!=operator<

compares error_conditions and error_codes (function)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the pair (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the tuple (function template)

operator==operator!=

compares the contents (public member function of std::bitset)

operator==operator!=

compares two allocator instances (public member function of std::allocator)

operator==operator!=operator<operator<=operator>operator>=

compares to another unique_ptr or with nullptr (function template)

operator==operator!=operator<operator<=operator>operator>=

compares with another shared_ptr or with nullptr (function template)

operator==operator!=

compares an std::function with nullptr (function template)

operator==operator!=operator<operator<=operator>operator>=

compares two durations (function template)

operator==operator!=operator<operator<=operator>operator>=

compares two time points (function template)

operator==operator!=

compares two scoped_allocator_adaptor instances (public member function of std::scoped_allocator_adaptor)

operator==operator!=operator<operator<=operator>operator>=

compares the underlying std::type_info objects (public member function of std::type_index)

operator==operator!=operator<operator>operator<=operator>=

lexicographically compares two strings (function template)

operator==operator!=

equality comparison between locale objects (public member function of std::locale)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the array (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the deque (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the forward_list (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the list (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the vector (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the map (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the multimap (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the set (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the multiset (function template)

operator==operator!=

compares the values in the unordered_map (function template)

operator==operator!=

compares the values in the unordered_multimap (function template)

operator==operator!=

compares the values in the unordered_set (function template)

operator==operator!=

compares the values in the unordered_multiset (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the queue (function template)

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the stack (function template)

operator==operator!=operator<operator<=operator>operator>=

compares the underlying iterators (function template)

operator==operator!=operator<operator<=operator>operator>=

compares the underlying iterators (function template)

operator==operator!=

compares two istream_iterators (function template)

operator==operator!=

compares two istreambuf_iterators (function template)

operator==operator!=

compares two complex numbers or a complex and a scalar (function template)

operator==operator!=operator<operator<=operator>operator>=

compares two valarrays or a valarray with a value (function template)

operator==operator!=

compares the internal states of two pseudo-random number engines (function template)

operator==operator!=

compares two distribution objects (function)

operator==operator!=operator<operator<=operator>operator>=

compares two sub_match objects (function template)

operator==operator!=

lexicographically compares the values in the two match result (function template)

operator==operator!=

compares two regex_iterators (public member function of std::regex_iterator)

operator==operator!=

compares two regex_token_iterators (public member function of std::regex_token_iterator)

operator==operator!=operator< operator<= operator> operator>=

compares two thread::id objects (function)

operator!=operator>operator<=operator>=

automatically generates comparison operators based on user-defined operator== and operator< (function template)

另见

运算符优先...

操作者超载...

公共算子

*。

分配增量递减算术逻辑比较成员访问其他

a=b a+=b a-=b a%2A=b a/=b a%=b a&=b a=b a^=b a<=b a>>=b.+a-a+a-+a-+a-a+b a-b a%2Ab a/b a%b~a&b ab^b a<<b a>>b.%21 a&b a&b ab a=b a%21=b a<b a>b a<=b a>=b a乙%2AA&A->b A.B a->%2Ab a.%2AA%28...%29 a,b?*

特殊运算符

静态[医]强制转换将一种类型转换为另一种相关类型动态。[医]继承层次结构中的强制转换[医]强制转换添加或删除cv限定符,重新解释[医]CAST将类型转换为不相关的类型C风格的强制转换通过混合静态方式将一种类型转换为另一种类型[医]卡斯特[医]重释[医]强制转换新创建具有动态存储持续时间的对象,删除删除以前由新表达式创建的对象,并释放获得的内存区域大小查询类型的大小...查询参数Pack%28的大小,因为C++11%29 Tyid查询类型no的类型信息,除了检查。表达式可以抛出异常%28,因为C++11%29查询对齐要求类型为%28,因为C++11%29。

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com