Comparison operators
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
You can help to correct and verify the translation. Click here for instructions.
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); |
|
Índice |
[editar]Explicação
You can help to correct and verify the translation. Click here for instructions.
[editar]Comparação operadores aritméticos
L
e R
, incluindo os tipos de enumeração, as assinaturas de função seguintes participar na resolução de sobrecarga:L
and R
, including enumeration types, the following function signatures participate in overload resolution:You can help to correct and verify the translation. Click here for instructions.
bool operator<(L, R); | ||
bool operator>(L, R); | ||
bool operator<=(L, R); | ||
bool operator>=(L, R); | ||
bool operator==(L, R); | ||
bool operator!=(L, R); | ||
You can help to correct and verify the translation. Click here for instructions.
[editar]Exemplo
#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'; unsignedint u =1;std::cout<<"Comparing signed and unsigned:\n"<<" -1 < 1? "<<(n < u)<<'\n'<<" -1 > 1? "<<(n > u)<<'\n'; unsignedchar uc =1;std::cout<<"Comparing signed and smaller unsigned:\n"<<" -1 < 1? "<<(n < uc)<<'\n'<<" -1 > 1? "<<(n > uc)<<'\n';}
Saída:
-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
[editar]Comparação operadores ponteiro
P
que é ou ponteiro para objeto ou ponteiro para função ou std::nullptr_t, e para cada MP
tipo que é um ponteiro para o objeto membro ou ponteiro para função de membro, as assinaturas de função seguintes participar na resolução de sobrecarga: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:You can help to correct and verify the translation. Click here for instructions.
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); | ||
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
p
e q
p
and q
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
p
e q
ponto aos membros da mesma matriz e a[i]a[j] ou um após o fim da matriz, que resulta da comparação entre os ponteiros é o mesmo que o resultado da comparação dos índices: i<j==true se então {{{1}}}.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}}}.You can help to correct and verify the translation. Click here for instructions.
p
e q
ponto para membros não-estáticos de dados dentro de uma mesma classe ou subobjetos base diferentes dentro da mesma classe derivada, ou aos seus membros ou subobjetos, recursivamente, e se os membros apontados / subobjetos ter o controle de acesso à mesma (por exemplo, tanto public:
), e da classe não é uma união, em seguida, o ponteiro para o subobjeto depois declarou / membro compara maior do que o ponteiro para o subobjeto anteriormente declarados / membro. Em outras palavras, os membros da classe em cada um dos três modos de acesso são posicionados na memória, a fim de declaração.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.You can help to correct and verify the translation. Click here for instructions.
p
e q
ponto aos membros da mesma union, comparam igual (tipicamente uma conversão explícita para void* é necessária para um dos operandos)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)You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[editar]Exemplo
#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';}
Saída:
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
[editar]Notas
You can help to correct and verify the translation. Click here for instructions.
operator<
é ordenação fraca rigorosa. Em particular, isso é exigido pelos algoritmos padrão e recipientes que trabalham com tipos LessThanComparable
: std::sort, std::max_element, std::map, etcoperator<
is ordenação fraca rigorosa. In particular, this is required by the standard algorithms and containers that work with LessThanComparable
types: std::sort, std::max_element, std::map, etc.You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Esta seção está incompleta Motivo: equivalence vs. equality |
[editar]Biblioteca padrão
You can help to correct and verify the translation. Click here for instructions.
checks whether the objects refer to the same type (of std::type_info função pública membro) | |
compara dois error_code sOriginal: compares two error_code sThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
compara error_conditions e error_codes Original: compares error_conditions and error_codes The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
lexicographically compara os valores do par Original: lexicographically compares the values in the pair The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na tupla Original: lexicographically compares the values in the tuple The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara o conteúdo Original: compares the contents The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (of std::bitset função pública membro) | |
compara duas instâncias alocador Original: compares two allocator instances The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (of std::allocator função pública membro) | |
compara a outra ou com unique_ptr nullptrOriginal: compares to another unique_ptr or with nullptrThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara com outro ou com shared_ptr nullptrOriginal: compares with another shared_ptr or with nullptrThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara com um std::functionstd::nullptr Original: compares an std::function with std::nullptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara duas durações Original: compares two durations The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois pontos de tempo Original: compares two time points The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara duas instâncias scoped_allocator_adaptor Original: compares two scoped_allocator_adaptor instances The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (of std::scoped_allocator_adaptor função pública membro) | |
compara os objetos std::type_info subjacentes Original: compares the underlying std::type_info objects The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (of std::type_index função pública membro) | |
lexicographically compara duas strings Original: lexicographically compares two strings The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
comparação de igualdade entre os objetos de localidade Original: equality comparison between locale objects The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (of std::locale função pública membro) | |
lexicographically compara os valores na array Original: lexicographically compares the values in the array The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na deque Original: lexicographically compares the values in the deque The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na forward_list Original: lexicographically compares the values in the forward_list The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na list Original: lexicographically compares the values in the list The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na vector Original: lexicographically compares the values in the vector The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na map Original: lexicographically compares the values in the map The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na multimap Original: lexicographically compares the values in the multimap The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na set Original: lexicographically compares the values in the set The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na multiset Original: lexicographically compares the values in the multiset The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara os valores na unordered_map Original: compares the values in the unordered_map The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara os valores na unordered_multimap Original: compares the values in the unordered_multimap The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara os valores na unordered_set Original: compares the values in the unordered_set The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara os valores na unordered_multiset Original: compares the values in the unordered_multiset The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na queue Original: lexicographically compares the values in the queue The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
lexicographically compara os valores na stack Original: lexicographically compares the values in the stack The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois reverse_iterators para a igualdade Original: compares two reverse_iterators for equality The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
reverse_iterators ordens Original: orders reverse_iterators The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois move_iterator s Original: compares two move_iterator s The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois istream_iterators Original: compares two istream_iterators The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois istreambuf_iterators Original: compares two istreambuf_iterators The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois números complexos ou um complexo e um escalar Original: compares two complex numbers or a complex and a scalar The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois valarrays ou um valarray com um valor Original: compares two valarrays or a valarray with a value The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara os estados internos de dois motores de números pseudo-aleatórios Original: compares the internal states of two pseudo-random number engines The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
compara dois objetos de distribuição Original: compares two distribution objects The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
lexicographically compara os valores no recipiente Original: lexicographically compares the values in the container The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
lexicographically compara os valores em dois o resultado da partida Original: lexicographically compares the values in the two match result The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois regex_iterator s Original: compares two regex_iterator s The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois regex_token_iterator s Original: compares two regex_token_iterator s The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) | |
compara dois objetos thread::id Original: compares two thread::id objectsThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
automatically generates comparison operators based on user-defined operator== and operator< (modelo de função) |
[editar]Veja também
Operadores comuns | ||||||
---|---|---|---|---|---|---|
assinamento | incremento descremento | aritmético | lógico | comparação | acesso de membro | outros |
a = b | ++a | +a | !a | a == b | a[b] | a(...) |
Operadores Especiais | ||||||
static_cast converte um tipo a um outro tipo relacionado |