Summary of C + + dots (I)
explain
The main reason why C + + is difficult to learn is that its standards and knowledge points are complex; For the previous C + + learning bias theory, this column will take every 20 small problems as an article, which mainly records the problems encountered in practice. The difficulty is different, which can be referred according to the title;
1. What is the function of file name suffix?
A general project contains multiple cpp (source file) and. h (header file);
There are some suffixes c . cc . How did the cxx file come from?
A: it is caused by different compilers. Generally, there are these suffixes under the GNU compiler;
. What is the function of hpp suffix?
A: actually h and cpp files are combined to reduce the compilation process of the whole project;
2. A brace initialization after C++11?
Initialization can be achieved by appending braces to variables:
int a{16}; int a[] {1,2 3, 4, 5}; // The middle equal sign can be omitted
3. What is a header file defensive declaration?
It is to prevent redefinition caused by repeated reference of header files;
Specific method: add conditional compilation in each header file (identifier settings in different header files should be different)
#ifndef __head1__ #define __head1__ // Code snippet #endif
4. How to understand quotation?
Essence: reference is the meaning of alias, which plays the role of an identifier;
Some notes:
-
The definition reference does not occupy additional memory, which can be understood as that the reference and the original variable occupy the same block of memory;
-
When defining a reference, it must be initialized and cannot bind constants;
5. View variable shortcuts in Visual Studio?
In the Debug mode, use shift+F9 to view the value given by the face change, including the address;
6. cout flushes the buffer?
Refreshing the buffer means printing to the terminal;
① The buffer is full;
② The program executes the return statement to main;
③ Call std::endl to forcibly refresh the buffer;
④ When the system is not busy, it will also view the contents of the buffer and output it to the terminal normally;
7. How to use the range for statement?
First, let's look at the normal use:
int x[]{1, 2, 3, 4}; for (auto v:x) { cout << v <<endl; }
In fact, the above code has a point that can be optimized. In the above code, the copy structure is executed, and the value of x is assigned to v, which can be modified as follows:
for (auto &v:x) { cout << v << endl; }
The alias is used to remove the copy process and improve the system efficiency;
8. C + + memory distribution?
It can be simply understood as five areas:
1. Stack: the area where local variables are placed inside a function, which is automatically allocated and released by the compiler;
2. Heap: allocated by the programmer malloc and new, and then released by free and delete; (forget to release, and the system will recycle after the program is completed)
3. Global / static storage area: global variable and static variable, which are released by the system at the end of the program;
4. Constant storage area;
5. Program code area;
9. Use of malloc?
First, it should be noted that malloc is a legacy of C. in C + +, it is recommended to use new and delete instead of malloc and free;
Function prototype:
void *malloc(int NumBytes); // NumBytes indicates the number of bytes to allocate
Return value: a pointer to the allocated memory is returned if successful, and NULL if failed;
If used, you also need to define a pointer to receive:
// Allocate memory for int * int *p = NULL; p = (int *)malloc(sizeof(int)); if (p != NULL) { *p = 5; } // Allocate memory for char * char *p = NULL; p = (char *)malloc(100 * sizeof(char)); if (p != NULL) { strcpy_s(p, 10, "hello"); // The middle number represents the allocated size. If it is less than the size of the incoming string, an error will be reported }
Let's look at the use of a simple pointer:
int *p = (int *)malloc(sizeof(int) * 100); // Allocate memory space of 100 shaping space sizes if (p != NULL) { int *q = p; // The current p value represents the first address of memory *q++ = 1; // Since + + and * have the same priority (right combination), it is understood here as * (Q + +), that is, q is assigned to 1, and then move an integer byte bit backward *q++ = 2; cout << *p << endl; // 1 cout << *(p+1) << endl; // 2 }
You need to understand this code enough. The bottom layer of C + + is pointer operation, including reference and pointer operation;
10. Use of new and delete?
First, explain the difference between malloc and malloc. Malloc and free do more initialization work than malloc and free;
There are three general forms:
① Pointer variable name = new type identifier;
② Pointer variable name = new type identifier (initial value);
③ Pointer variable name = new type identifier [number of memory units];
// Method 1 int *p = new int; if (p != NULL) { *p = 16; cout << *p << endl; delete p; } // Method 2 int *p = new int(16); // Method 3 int *p = new int[100]; // Open up a size of 100 integer array space if (p != NULL) { int *q = p; *q++ = 1; *q++ = 2; delete[] p; // Note that [] }
11. Is there a difference between NULL and nullptr?
In fact, nullptr is used to prevent the confusion of integer and pointer types, but they are equal through if judgment;
int *p = NULL; int *q = nullptr; if(p == q){} // The comparison of the two is equal // The types of printing are different cout << typeid(NULL).name() << endl; // int cout << typeid(nullptr).name() << endl; // std::nullptr_t
12. What are the main uses of private?
Function: from the perspective of actual code, it is generally used to set function variables as private, and these variables are only accessible to internal member functions;
13. What are the differences between classes and structures in C + +?
First of all, it is best to use classes in C + +. After all, they are introduced after C + +;
The difference is that the default access level of member functions and member variables in the class is private, and the default access level in the structure is pubilc;
14. Post return type function?
When you brush the Leetcode title, you often see that the return type is after the parameter list. In fact, this writing method is OK. Record here:
auto fun(int a) -> void;
It only needs to be understood, not used generally;
15. The role of inline functions?
Add the keyword inline before the function to make the function become an inline function;
Problem solving: such functions with small function body and frequent calls (frequent calls will push the stack out of the stack, consume memory and have low efficiency);
effect:
① Inline can affect the compiler. In the compilation stage, the inline function is processed, and the action of calling the function is replaced by the function ontology;
② inline is only the developer's suggestion to the compiler. Whether to replace it is up to the compiler (the decision is up to the compiler)
③ The definition (ontology) of the inline function needs to be written in the header file; (include can try to replace the source code)
④ constexpr function can be regarded as a strict inline function;
Disadvantages: it may cause code inflation, so the function body should be as small as possible;
16. Some hybrid uses of functions?
① The return type of the function is void, which means that the function does not return any type; You can return a function whose return type is void;
void funca(){} void funcb() { return funca(); }
② The return value of the function is a pointer
int *func() { int tmp = 16; return &tmp; }
The address of a local variable returned by the above function is incorrect. After calling the function, the local variable has been recycled by the system. At this time, the returned address is not a controllable address, which may cause problems;
③ The function has no formal parameters. You can keep the formal parameter list empty (), or (void);
④ If we do not call a function, the function can only be declared but not defined;
⑤ For ordinary functions, the definition can only be defined once (in the. cpp file), and the declaration can be declared multiple times;
⑥ In C + + function parameters, it is recommended to use more reference forms, which can reduce copy construction and improve efficiency;
17. Some differences between const and pointer combination during initialization?
Think about the difference between the three ways of writing?
char str[] = "hello"; # Writing method I const char* p = str; # Writing method 2 char const *p = str; # Writing method III char * const p = str; p++; // Wrong writing
Among the above three writing methods, the first and second writing methods have the same function. They can not modify the content of P pointing to the target, but can modify the pointing of p;
The third way of writing is that the direction of the pointer cannot be changed, and the content of the target pointed by the pointer can be modified;
18. Function with const in function parameters?
Function: it is not allowed to modify the value of formal parameters. Generally, all parameters that can be added should be added;
Benefits:
① It can prevent unintentional modification of the value of the formal parameter, resulting in the modification of the argument;
② More flexible argument types; (mainly because there are more supported types)
19. How many initialization methods of string?
As the type of C + + standard library, string is one of the most commonly used types;
There are several initialization methods:
#include<string> using namespace std; string s; // Default initialization, s = "" empty string, indicating that there are no characters; string s1 = "Hello World!"; // Copy the string to s1 memory, excluding the \ 0 at the end; string s2("Hello World"); // The effect is consistent with that of s1; string s3 = s1; // Copy the contents of s1 to a section of memory represented by s3; int num = 10; string s4(num, 's'); // Ssssssss, a string composed of 10 s, is not commonly used;
20. Common methods of string
There are some common methods:
string s1; // Air judgment s1.empty() // Returns the number of bytes / characters representing the length of the string s1.size(); s1.length(); // Index, similar to array s1[n]; // Returns the content pointer in the string s1, ending with \ 0. In fact, to be compatible with the C language, the string object is converted to the style in the C language const char *p = s1.c_str(); char s1[] = "hello"; // This is the character array definition left over from C language // Scope for handling string objects for(auto &a : s1) { a = 'i'; // Change all characters to i to directly change the incoming data }