C + + bottom line (continuous update)

Time:2022-1-19

Header file

The difference between “” and < >

< > find the header file in the system directory first. If not, find it in the current directory。 So like the standard header file stdio h、stdlib. H et al. Use this method.

“” first look in the current directory. If it cannot be found, then look in the system directory。 This header file for include customization allows the system to preferentially use the header file defined in the current directory.

Belt H and without H difference

The header file before C + + standardization is the file with suffix, and the header file after standardization is the file without suffix。 C + + 98 provisionsUsers should use the new header file, there is no mandatory specification for the old version header files, but most compiler manufacturers still provide the old version header files for downward compatibility.

C + + standardization adds the concept of namespace to declare the identifier originally declared in global space under namespace STD.Use new header files (e.g. #include))When using, use namespace STD before using the function; Import namespaces and use legacy header files (such as #include))No

Difference between C prefix and no C prefix

In order to be compatible with C language, in the process of C + + standardization,After the original C language header file is standardized, the header file name is preceded by a c letter, such as cstdio, CString, CTime, ctype, etc. These header files can be found in the system directory (such as C: \ program files (x86) \ Microsoft Visual Studio 14.0 \ VC \ include).

If you want to use the C language header file standardized by C + +, you must make the following conversion:

#include  --> #include 
#include  --> #include 
#include  --> #include

Macro definition

#Define command is a macro definition command in C language. It is used to define an identifier as a string. The identifier is called macro name, and the defined string is called replacement text.
The command has two formats:One is a simple macro definition, the other is a macro definition with parameters

  • Simple macro definition:
#define

Example: #define PI 3.1415926

  • Macro definition with parameters
#define  ()

Example: #define a (x) x

When an identifier is defined by a macro, the identifier is a macro name. Before the program is compiled, replace the macro name with the defined string, which is called macro replacement, and then compile,Macro replacement is a simple replacement

Special symbols: #, ###@

  • #x: double quote x
#define ToString(x) #x

//str="123132";
char* str = ToString(123132);
  • x##yY indicates x connection:
#define Conn(x,y) x##y

//n=123456;
int n = Conn(123,456);
  • #@x: single quote x
#define ToChar(x) #@x

//a='1';
char a = ToChar(1);
//Error reporting
char a = ToChar(123);

Prevent a header file from being included repeatedly

#ifndef BODYDEF_H 
#define BODYDEF_H 
 //Header file content 
#endif

Do while action

A characteristic of do while statement is that it will execute the statement block first, and then judge whether the conditions in while are true. When we set while to while (0), the while condition will never hold, and the code in the statement block after do will only be executed once.

The multi line code defined by the macro is surrounded by do {/ *… * /} while (0), which can reduce ambiguity:

#include 

#define MACRO_FUNC() \
    do \
    { \
        printf("Hello\n"); \
        printf("World\n"); \
    } while(0)
/*According to the above example, we should not add a semicolon at the end of the macro definition*/

int main()
{
    if(2 > 1)
        MACRO_FUNC();
    else
        printf("Fail.\n");
    return 0;
}

If you do not use do while or only use {}, the following else part will inevitably report an error.
Note:When writing a macro definition function, you need to use the continuation character – backslash ”

Usage of undef

When you run out of a macro and don’t want the following code to use it again, you can #undef it. If you use this macro again, the compiler will report an error. Common practices are:

#define MAX 50
#include "common.h"
#undef  MAX

So only in common The macro Max can only be used in H.

Bracketed function

Some functions need to be enclosed in parentheses when used, such as:

cout << (numeric_limits::min)() << endl;

Because Microsoft is in windef Two macros Max and min are defined in H,When a function name has the same name as a macro, it is either #undef or enclosed in parentheses

reference material

Recommended Today

Modify user information changeinfo

When judging the persistence layer: Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be […]