In Noip, macros are often used, so here's a look at the definition and usage of macros in C + +
- first Usage--match conditional compilation : #define DEBUG
Defines a identifier called Debug. It should be used in conjunction with #ifdef or #ifndef. Examples are as follows:
#define DEBUG#IFDEF debugvoid Print (int v) {cout << v << Endl;} #elsevoid print (int) {} #endif
If the symbol debug is present, then the compiler compiles the above print with the output value, otherwise the compiler compiles the print below and does nothing.
Replace the above #ifdef with #ifndef, then the compiled code is just the opposite of what is said above.
2. The second usage-expression :
#define N 5000
At compile time, the compiler uses a method similar to "Find and replace" to replace N in the code with 5000. If you need to change to an expression, enclose them in parentheses. For example:
#define a 1+2#define b (1+2) c=a*2; d=b*2;
At compile time the above line will become "C=1+2*2; d= (1+2) * *, obviously their values are different.
Also, be aware that there can be no semicolon at the end of an expression (unless you want to).
3. The third use-simple "function":
#define FtoC (A) (((a)-32)/9*5)
This is similar to a function. However, because the compiler only makes simple substitutions, for security purposes, A and B should be surrounded by parentheses, and the entire expression should be surrounded by parentheses.
This "function" usage is the same as a normal function and is faster. However, it is prone to difficult to detect errors. Therefore, use the inline function (inline) instead of the macro definition.
Note that you do not change the value of the variable in Parameters !
4. Fourth usage-simplify a piece of code :
#define MOVE (dx, dy) if (Isfull (dir)) return; if (Map (X+DX, y+dy) = = ' 0 ') {push (Dir,x+dx,y+dy,head[dir], DEP); check (dir);}
Don't forget "\" after each line, which is equivalent to a newline character. This move simplifies a large piece of code. Of course, inline functions in C + + can also be implemented in this function.
Definition and usage of macros in C + + (now replaced by inline functions)