[C++ problem collection - continuous update]

0. Header file inclusion order

Using a standard header file include order enhances readability and avoids hidden dependencies.
The recommended order of "C++ Programming Ideas": (for xxx.cpp) from the most special to the most general.

xxx.h
本项目内 .h 文件
其他库的 .h 文件
C++ 系统文件
C 系统文件

"Google C++ Style Guide" recommended order: (for xxx.cpp)

xxx.h
C 系统文件
C++ 系统文件
其他库的 .h 文件
本项目内 .h 文件

The same thing between the two is to put xxx.h at the beginning to avoid hidden dependencies. We hope that each header file can be compiled independently.

1. Memory access violation

When the class member counts is not initialized, errors of heap corruption and memory access violation occur. The main reason is that when it comes to the use of this member later, it gets a value randomly assigned by the system (relatively large), and when the memory is indexed according to this parameter, it is caused by accessing memory that does not belong to itself.
Lesson: All class members must be initialized before use
insert image description here
insert image description here

2. Global variables

Visible from the beginning of the declaration to the end of the file, it is stored in the global/static area. (Local variables are generally only local variables inside the function)
(1) A single file
can be defined outside the function
(2) Multiple files: irrelevant and related (referring to header files)
a. Related (referring to global variable definitions file)
declaration of global variables:

extern int g_a;

Definition of global variables:

g_a = 0;

Note: There can be multiple declarations of the same variable or function, but only one definition.
Avoid by all means: put the declaration and definition together, so that when the header file is included, there will be a repeated definition error extern int g_a=0.
b. Irrelevant
declarations and definitions are in the same file, such as extern int g_a=0 in test1.h;
test1.h is not included in test2, and the declaration extern int g_a is required to use this global variable;

3. Precautions for Float and double types

When performing addition, due to the limited number of effective digits, the addition will end.
For the float type, when the value is greater than 1*10^7, avoid using addition;
insert image description here
insert image description here

For the double type, when the value is greater than 1*10^16, avoid using addition.
insert image description here
insert image description here

-------------------------------------------------- ------------------------2023.02.23 Update---------------------- -------------------------------------------------- --

4. The truncation problem of floating-point conversion to integer

Float conversion to int type will cause truncation, such as storing 400, float type is 399.99998, and 399 after truncation conversion. Therefore, when converting a floating-point type to an integer type, it is best to convert after rounding, float(round(a)).

5. The difference between NULL and nullptr

In C++, NULL and nullptr, in C language, NULL means a null pointer, while in C++, NULL means 0 or a null pointer, and nullptr is after C++11, which means a null pointer. Therefore, the use of null pointers in C++ is recommended to use nullptr uniformly.

6. Convert between string and char

string to char

char ch[20];
string s="123456";
strcpy(ch,s.c_str());

string to char *

const char* p=a.data();
const char* p=a.c_str();
char* p=const_cast<char*>(a_str());
char转string直接赋值即可

7. Convert between string and int

string to int, first to char and then to int

string str1 = s_Value.toStdString();
int value = atoi(str1.c_str());
float value = atof(str1.c_str()); //转float

int to string (c++11 standard adds the global function std::to_string)

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
std::string pi = "pi is " + std::to_string(3.1415926);

Guess you like

Origin blog.csdn.net/qq_44924694/article/details/127939724