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. |
<metanoindex/>
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);
|
| ||||
Explication
You can help to correct and verify the translation. Click here for instructions.
Opérateurs arithmétiques
L et R, y compris les types d'énumération, les signatures de fonction suivants participent à la résolution de surcharge: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.
Exemple
#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';
}
Résultat :
-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
Les opérateurs de comparaison de pointeurs
P qui est soit pointeur à l'objet ou au pointeur de fonction ou std::nullptr_t, et pour chaque type de MP qui est un pointeur vers l'objet membre ou un pointeur vers une fonction membre, les signatures de fonction suivants participent à la résolution de surcharge: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); |
||
operator== et operator!= uniquement) ou un pointeur et un pointeur NULL constante, ou deux constantes pointeur nul (mais seulement dans la mesure où au moins l'un d'entre eux est std::nullptr_t: comparaison de NULL et NULL suit des règles arithmétiques). Conversions de pointeurs (pointeur vers conversions membres si les arguments sont des pointeurs vers des membres) et conversions de qualification sont appliquées aux deux opérandes afin d'obtenir l composite de type pointeur, comme suitoperator== 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). Conversions de pointeurs (pointer to member conversions if the arguments are pointers to members) and conversions de qualification are applied to both operands to obtain the composite pointer type, as followsYou 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.
void* .void*.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 et 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.
p==q, p<=q et p>=q retour true, tandis que p!=q, p<q et p>q retour false,p==q, p<=q, and p>=q return true, while p!=q, p<q, and p>q return false,You can help to correct and verify the translation. Click here for instructions.
p==q retours true, p!=q retours false, le comportement des autres opérateurs n'est pas spécifié .p==q returns true, p!=q returns false, the behavior of other operators is unspecified.You can help to correct and verify the translation. Click here for instructions.
p et q point à des membres de la même matrice ou a[i] a[j] et une delà de l'extrémité de la rangée, ils sont comparés aux résultats de pointeurs est le même que le résultat de la comparaison des indices: si i<j==true puis {{{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 et q point de non-membres de données statiques au sein de la même catégorie ou sous-objets de base différentes dans la même classe dérivée, ou à leurs membres ou sous-objets, de manière récursive, et si les membres pointés / sous-objets ont le même contrôle d'accès (par exemple, à la fois public:), et la classe n'est pas un syndicat, alors le pointeur sur le sous-objet par la suite déclaré / membre supérieur compare le pointeur sur le sous-objet précédemment déclarées / membre. En d'autres termes, les membres du groupe dans chacun des trois modes d'accès sont placées en mémoire dans l'ordre de déclaration .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 et le point q aux membres de la union même, elles sont égales (typiquement une conversion explicite pour void* est requis pour l'un des opérandes)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.
void et les deux pointeurs pointent à la même adresse ou les deux sont des pointeurs nuls, ils comparent l'égalité .void and both pointers point to the same address or are both null pointers, they compare equal.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<q, p>q, p<=q et p>=q ne sont pas précisés, et p!=q retours false.p<q, p>q, p<=q, and p>=q are unspecified, and p!=q returns false.You can help to correct and verify the translation. Click here for instructions.
Exemple
#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';
}
Résultat :
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
Notes
a<b<c expression est analysée (a<b)<c, et non a<(b<c) ou (a<b)&&(b<c) .a<b<c is parsed (a<b)<c, and not a<(b<c) or (a<b)&&(b<c).You can help to correct and verify the translation. Click here for instructions.
operator< est strict ordonnancement faible. En particulier, cela est requis par les algorithmes standards et des conteneurs qui travaillent avec des types LessThanComparable: std::sort, std::max_element, std::map, etcoperator< is strict ordonnancement faible. 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.
| This section is incomplete Reason: equivalence vs. equality |
Bibliothèque standard
You can help to correct and verify the translation. Click here for instructions.
| checks whether the objects refer to the same type (fonction membre publique de std::type_info)
| |
compare deux error_codesOriginal: compares two error_codesThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction) | |
compare error_conditions et 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. (fonction) | |
compare lexicographiquement les valeurs de la paire 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le tuple 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. (fonction générique) | |
compare le contenu 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. (fonction membre publique de std::bitset)
| |
compare deux instances du programme d'allocation 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. (fonction membre publique de std::allocator)
| |
compare à un autre ou avec unique_ptr nullptr Original: compares to another unique_ptr or with nullptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) | |
compare avec un autre ou avec shared_ptr nullptr Original: compares with another shared_ptr or with nullptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) | |
compare avec une std::function std::nullptrOriginal: compares an std::function with std::nullptrThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) | |
compare deux duré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. (fonction générique) | |
compare deux points dans le temps 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. (fonction générique) | |
compare deux instances 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. (fonction membre publique de std::scoped_allocator_adaptor)
| |
compare les objets sous-jacents std::type_info 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. (fonction membre publique de std::type_index)
| |
compare lexicographiquement deux chaînes 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. (fonction générique) | |
comparaison d'égalité entre les objets locale 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. (fonction membre publique de std::locale)
| |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare les valeurs de la 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. (fonction générique) | |
compare les valeurs de la 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. (fonction générique) | |
compare les valeurs de la 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. (fonction générique) | |
compare les valeurs de la 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare lexicographiquement les valeurs dans le 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. (fonction générique) | |
compare deux reverse_iterators pour l'égalité 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. (fonction générique) | |
reverse_iterators commandes 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. (fonction générique) | |
compare deux move_iterators Original: compares two move_iterators The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) | |
compare deux 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. (fonction générique) | |
compare deux 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. (fonction générique) | |
compare deux nombres complexes ou un complexe et un scalaire 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. (fonction générique) | |
compare deux valarrays ou un valarray avec une valeur 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. (fonction générique) | |
compare les états internes des deux moteurs de nombres pseudo-aléatoires 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. (fonction) | |
compare deux objets de distribution 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. (fonction) | |
compare les valeurs lexicographiquement dans le récipient 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. (fonction) | |
compare lexicographiquement les valeurs dans le résultat deux match 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. (fonction générique) | |
compare deux regex_iterators Original: compares two regex_iterators The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) | |
compare deux regex_token_iterators Original: compares two regex_token_iterators The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) | |
compare deux objets thread::idOriginal: 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. (fonction) | |
automatically generates comparison operators based on user-defined operator== and operator< (fonction générique) | |
Voir aussi
| Opérateurs ordinaires | ||||||
|---|---|---|---|---|---|---|
| affectation | incrémentation décrémentation |
arithmétique | logique | comparaison | accès aux membre | autre |
|
|
|
|
|
|
|
|
| Opérateurs spéciaux | ||||||
|
static_cast convertit un type dans un autre type compatible dynamic_cast convertit une classe de base virtuelle dans une classe dérivée const_cast convertit un type dans un type compatible avec des cv-qualifiers différents reinterpret_cast convertit un type dans un type incompatibles new allocation de la mémoire delete libère de la mémoire sizeof récupère la taille d'un type sizeof... récupère la taille d'un paquet de paramètres (depuis C++11) typeid récupère les informations de type d'un type noexcept vérifie si une expression peut lancer une exception (depuis C++11) alignof récupère les conditions d'alignement d'un type (depuis C++11) | ||||||