introduction
Beginners in informatics competitions are always prone to making some low-level mistakes - forgetting file IO, forgetting to delete debug information, variable names, function names conflicts, etc. At the same time, when contestants need to write some points or encounter some special situations (such as programs need to select different calculation plans based on different data ranges), some tools are also needed to optimize the organizational structure of the code. At this time, macro definition becomes an effective tool.
text
use
Macro definition is a preprocessing directive in C and C++ that allows programmers to define an identifier in source code so that the identifier can be used instead of a specific value or code block in subsequent code. Macro definitions are processed by preprocessors before code compilation, which is similar to code replacement, helping to improve the readability and maintainability of the code. Its usage is like this:
#define <macro name>(parameter) <string>
For example,#define int long long
Can be compiled at the time ofint
Replace the string withlong long
。
These statements are commonly used in conjunction with them:
Statement | meaning |
---|---|
#ifdef <macro name> |
Check if the macro is defined |
#ifndef <macro name> |
Check if the macro is not defined |
#endif |
Asifdef andifndef End sign |
#else |
Asifdef andifndef The Negation |
#undef <macro name> |
Cancel macro definition |
That is, the macro definition will "replace" the representative in the code. Here is an example of writing A+B problem code using macro definitions:
#include <iostream>
#define int long long // Macro definition int
int a, b, c; // Here will be compiled according to long long
#ifdef int
#undef int // Macro definition int end
#endif
#define ADD(x,y) x+y // Macro definition ADD
int main() { // Here will be compiled according to int
#ifndef ONLINE_JUDGE
// If -DONLINE_JUDGE is used during compilation, file reading and writing will not be used
freopen("", "r", stdin);
freopen("", "w", stdout);
#endif
std::cin >> a >> b;
c = ADD(a,b); // Here will be compiled according to a+b
std::cout << c << std::endl;
return 0;
}
It can be seen that macro definitions can assist in writing code and reduce the probability of losing points due to problems such as forgetting to read and write files. Making good use of macro definitions can quickly debug code in large exams and solve some problems that are not easy to solve by conventional methods.
Things to note
- Compute priority. For example, if
pf(x)
forx*x
,butpf(a+b)
Will be replaced witha+b*a+b
, may not meet expectations, so it may be necessary to add brackets to adjust the priority of operations. For example, if you want to make the replacement result become(a+b)*(a+b)
, the definition may need to be written as#define pf(x) (x)*(x)
。 - The number of times the parameter is transferred. For example, if
#define ABS(x) (x>0)?x:-x
,butABS((pd()))
Will be replaced with((pd())>0) ? (pd()) : -(pd())
,lead topd
Being executed\(2\)Second-rate. ifpd
The operation result changes will cause errors, and the program run time will be greatly increased.
Acknowledgements
gratefultristianoHelp for writing articles.