STL program design Practice 6: Customized types to define constant equality and less than operators
Yuan Xiakai
South China University of Technology Computer Research Institute North District R & D
Email - ccplusplus@21cn.com
The comparison operation is a very general operation, and it is also true when using STL. There are six types of comparison operations, namely: ==,! =,>, <,> =, <=. The C built-in data type supports these comparison operations. However, the user-defined operator is required when the custom type is compared, otherwise the comparison operation cannot be performed. Isn't it necessary to define all of these operators, in this case, but when you use STL to design, usually just define Operator == and Operator <即, unless you directly carry out two elements operating. Below we will make an example, please don't blame my example.
Operator == operator
When you use the Vector stored custom type in STL, you may do some operations, you must use the Vector to operate it. Such as:
INT A [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
INT B [10] = {1, 3, 3, 4, 5, 8, 7, 8, 9, 10};
Vector
Vector
IF (vobj == VOBJ2)
......
In the determination conditions of the IF, the elements in the two vectors are equal, and the vector needs to use the Operator == operator that needs to use the element when performing such comparison operations. INT is the built-in data type, supports Operator == operation. When you use a custom type, you must ensure that it has the correct definition of Operator ==. But if you need to do it, it is not equal to the comparison operation.
IF (Vobj! = VOBJ2)
......
In the determination condition of the IF, you will call the verator! = operator, but the manufacturer's Operator! = operator does not call the Operator! = operator of the element, but in the form of! (==) to call the outputor == operation symbol. Because in the arithmetic expression:
! (x == y) Means x! = y
Expressed X! = Y, saved! = And == OR operators, but also simplifies maintenance, simply modify operational ==. The most important thing is to reduce the requirements of the elements of the operation, the elements do not need to define the Operator! = Operator here.
2. Operator There are too many places in STL to use the Operator IF (vobj ...... The size of the two Vector objects is compared in the determination conditions of the IF. If an element in an object is larger than the corresponding element in another object, the vector needs to use an element when making such a comparison operation. Operator IF (Vobj> VOBJ2) ...... In the determination condition of the IF, the verator> operator of the Vector, but the manufacturer's Operator> operator does not invoke the Operator> operator of the element, but calls the verator Expressed (x> y), saved the relationship between> and A. x> = y mean! (X B. X <= y 味 意! (Y to sum up: There are too many places in STL to use these comparison operators. If your custom type correctly defines the Operator == and Operator Remnant: Writing this article is a little experience after watching the Vector in the STL source code. It is also my personal learning summary. If there is any mistake, please correct it. reference: 1.SGI STL source file STL_VECTOR.H line 522.