5.1 Arithmetic Operators
Arithmetic Operator priority-the highest level of the unary operator, followed by the multiplication and division operation, followed by the addition and subtraction operation of the binary element.
Remainder operation and division operation
Pay attention to positive and negative situations.
-21%-8 // result is-5 close to 0
21%-5 // result is 1 or-4
-21/-8 // result is 2 close to 0
21/-5 // result is-4 or-5
5.2 Relational operators and logical operators
Short Circuit value. logic and, logic or always calculate its left operand first, and then its right operand. The right operand is solved only when the value of the left operand cannot determine the value of the logical expression.
Exercise 5.8
#include<iostream>#define TRUE 1#define FAIL 0using namespace std;int main(){int a,b,c,d;cout<<"please input 4 intergers:"<<endl;cin>>a>>b>>c>>d;if(a>b&&b>c&&c>d){cout<<"Good."<<endl; return TRUE;} cout<<"bad."<<endlreturn FAIL;}
5.3-bit operator
Operator |
Description |
~ |
This is a bitwise inverse operator. It is a unary operator that can reverse the bitwise In the operand, that is, 1 is changed to 0, 0 is changed to 1. |
& |
This is a bitwise AND operator, which performs operations on the corresponding bitwise In the operand. If the corresponding bits are both 1, The result bit is 1; otherwise, the result bit is 0. |
^ |
This is a bitwise XOR operator that performs an exclusive or operation on the corresponding bitwise In the operand. If the corresponding bits are different, for example, if one bit is 1 and the other is 0, the result bit is 1. If the corresponding bits are the same, the result bit is 0. |
| |
This is a bitwise OR operator that performs or operations on the corresponding bitwise In the operand. If one of the two corresponding bits is 1, The result bit is 1. If both digits are 0, the result is 0. |
< |
Move left |
> |
Right Shift |
5.4 value assignment operator
Right combination, low priority
Note the difference between = and = !!!!
The value-assigned operand must be the left value of the non-const operation.
5.5 auto-incrementing auto-subtraction Operator
The post operator is used only when necessary! We need to develop a good habit of using prefix operators ~
5.6 arrow Operators
(* P). Foo;
P-> Foo; // The two sub-statements are the same
Exercise 5.18
# Include <iostream> # include <vector> # include <string> # define fail 0; # define true 1; using namespace STD; int main () {vector <string *> ivec; string STR; cout <"input some strings:" <Endl; while (CIN> Str) {string * pstr = new string; * pstr = STR; ivec. push_back (pstr);} vector <string *>: iterator iter = ivec. begin (); While (ITER! = Ivec. end () {cout <** ITER <(** ITER ). size () <Endl; ITER ++;} // release each dynamically allocated object iter = ivec. begin (); While (ITER! = Ivec. End () {Delete * ITER; ITER ++ ;}}
5.7 conditional Operators
Unique ternary operators in C ++
Exercise 5.20
# Include <iostream> # define true 1; using namespace STD; int main () {int val1, val2; cout <"input two intergers:" <Endl; cin> val1> val2; cout <(val1> val2 )? Val2: val1); // use parentheses to enclose condition operators !! Cout <(val1> val2 )? Val2: val1; // output 11 or 0 return true ;}
5.8sizeof Operator
The sizeof operator returns the length of an object or type name. The type returned is size_t.
Example:
Int SZ = sizeof (IA)/sizeof (* IA); // The returned value is the length of the array IA.
Returns the length of the built-in type.
#include<iostream>#define FAIL 0; #define TRUE 1;using namespace std;int main(){int intlen=sizeof(int); cout<<"int "<<intlen<<endl; intlen=sizeof(double); cout<<"double "<<intlen<<endl; intlen=sizeof(long); cout<<"long "<<intlen<<endl; intlen=sizeof(short); cout<<"short "<<intlen<<endl; intlen=sizeof(string); cout<<"string "<<intlen<<endl; intlen=sizeof(float); cout<<"float "<<intlen<<endl;cout<<sizeof intlen<<endl<<sizeof (intlen) ;return TRUE;}
5.9 comma Operator
The result of a comma expression is its right expression.
5.10 compound expression evaluate
Assign values to the right and combine arithmetic operations to the left. Pay attention to priority ~!!!!!!!!!
Parentheses can be used to avoid incorrect priority during beginners. They are divided into two independent statements.
In an expression, do not add or subtract the same object in two or more subexpressions.
Except for logic and logic, C ++ does not define the order of solving other binary operators. This is for efficiency consideration, because when only two operands involve the same object, the operation order will affect the calculation result.
5.11new and delete expressions
New and delete dynamically create and release arrays, or dynamically create and release a single object.
The new expression returns the pointer to the newly created object.
You must use the direct initialization syntax rules to initialize a dynamically created non-built-in type object.
Built-in types can be not initialized
For example:
Int I;
Int * P = new int; // Uninitialized Object
Int * IP = new int (); // initialize to 0
String * P = new string (9999999999) // * P is
Initialization is always a good practice.
Int I;
Int * P = new int;
Delete P;
After the pointer is deleted, the pointer becomes a hanging pointer, which is dangerous. Once the pointer is deleted, the pointer is immediately assigned to zero!
5.12 type conversion
Arithmetic Conversion
Short, char -----> int -----> unsigned -----> long --------> double
Float -----> double
Pointer Conversion
Int Ia [10];
Int * P = IA; // The IA array is converted to a pointer pointing to the first element.
Bool Conversion
Int CP = 10;
If (CP) // CP to bool type, non-zero is 1
Forced type conversion (avoid using forced type conversion)
Int A = 3.14; // A = 3
Dynamic_cast
Const_cast
Static_cast
Reinterpret_cast