The following describesC ++The four tips for programming are for your reference.
1. debugging mark
Applicable to preprocessing # define to define one or more debugging tags. In the code, use # ifdef and # endif for debugging management. After the program is finally debugged, you only need to use the # undef flag to debug the code. The commonly used debugging mark is DEBUG, and the statement sequence is as follows:
# Define DEBUG
# Ifdef DEBUG
Debug code
# Endif
2. debugging mark during running
Enable and disable the debugging flag during the running of the program. You can set a debug bool flag. This is more convenient for the program running on the command line.
For example, the following code
- # Include <iostream>
- # Include <string>
- Using namespace std;
- Bool debug = false;
- Int main (int argc, char * argv [])
- {
- For (int I = 0; I <argc; I ++)
- If (string (argv [I]) = "-- debug = on")
- Debug = true;
- Bool go = true;
- While (go)
- {
- If (debug)
- {
- Debug code
- } Else {}
- }
- }
3. Convert variables and expressions into strings
However, the string operator is used to define the conversion output.
- #define PR(x) cout<<#x”=”<<x<<'\n'
4. C language assert ()
In <assert>, when assert is used, a parameter is provided, that is, a true expression. The Preprocessor generates code to test the assertions. If the assertions are not true, an error message is sent to indicate what the assertions are and if they fail for a while, the program will terminate.
- # Include <assert>
- Using namsapce std;
- Int main ()
- {
- Int I = 100;
- Assert (I! = 100); // Fails
- }
- After debugging, add # define NDEBUG before # include <assert> to eliminate the code generated in red.
- }
I hope this article will help you. Let's take a look.