C++ record summary and interview frequently asked questions

pointers and functions

The function value transfer will copy the variable copy and increase the memory space. Changing the variable value inside the function will not affect the variable value of the main function (outside the function).
Passing a function pointer will not copy a copy of the variable, and the first byte of the pointer variable is fixed, and will not increase the memory space occupied. Changing the internal parameter value of the function will also change outside the function. If you do not want to modify the data inside the function or prevent misuse, you can pass a const pointer.
func(int a, int b) is passed by value, the formal parameter will not modify the actual parameter
func(int *a, int *b) is passed by address, the formal parameter will modify the actual parameter
func(int& a, int& b) is passed by reference, the formal parameter It will modify the actual parameter. If you don’t want to modify it, you can add const before the formal parameter

memory partition model

Before the program runs:
code area: stores the binary code of the function body and is managed by the operating system. Shared + read-only features.
Global area: Store global variables, static variables and constants.
After the program runs:
Stack area: automatically allocated and released by the compiler, storing function parameter values, local variables, etc. The address of the local variable cannot be returned, and the garbled code will be released the second time it is reserved for the first time.
Heap area: allocated and released by the programmer, if the programmer does not release it, it will be reclaimed by the operating system at the end of the program. The new keyword can be used to open up data memory to the heap area, manually open, manually release, the keyword is delete, if it is an array plus [], that is, delete[] the data stored in different areas, endowed with different life cycles, with
greater Flexible programming.

access permission

Public members can be accessed within the class, and can be accessed outside
the class. Protected can be accessed within the class, but cannot be accessed outside the class, and subclasses can access
private. Accessible within the class, not accessible outside the class, subclasses cannot access
member attributes/variables, set It is private, you can control the read and write permissions by yourself, and you can check the validity of the data. Read or write operations can be set through public member functions.

struct & class create class

struct default public permission
class default private permission

Constructor

Parameters: construction with parameters, construction without parameters (default construction)
Types: ordinary construction, copy construction Copy the passed-in attributes to yourself Usage: (class name (const class name & attribute)), note that copy cannot be used Constructor, initialized to an anonymous object, will be redefined.
Call the default constructor without adding (), if you add (), the compiler will consider it a function declaration.
Call method: 1. Bracket method, which is also an anonymous object. The anonymous object will be recycled immediately after the execution of the current line (call destructor) 2. Explicit method, use the bracket method to assign to the left parameter method, 3. Implicitly call the parameter structure , direct = assignment to the left reference.

static member

Including static member variables and static member functions
Static member variables:
1. All objects share a piece of data
2. Allocate memory in the compilation phase
3. In-class declaration and out-of-class initialization
There are two access methods: access by object and access by class name
Static member functions:
1. All objects share a function
2. Only static member variables can be accessed

this pointer

The this pointer points to the object to which the called member function belongs.
A pointer within every non-static member function is implied.
No need to define, just use it directly.
Purpose:
When the formal parameter and the member variable have the same name, the this pointer can be used to distinguish them. Similar to self in python,
return the object itself in the non-static member function of the class, use return *this

const modified member function

It is called a constant function, and member attributes cannot be modified in a constant function. (The essence of this pointer is a pointer constant, and the point of the pointer cannot be modified.)
If you want to modify it, you can add the keyword mutable to the member attribute declaration.
If you add const before the object declaration, it is a constant object.
Constant objects can only call constant functions.

friend friend

The purpose is to allow a function or class to access private members in another class.
1. Global functions as friends
2. Classes as friends
3. Member functions as friends

Overloading Operators and Overloading Functions

C++ allows multiple definitions of a function and operator to be specified in the same scope, known as function overloading and operator overloading, respectively.
Expression operators on built-in data types are immutable

inherit

Grammar: class subclass (derived class): inheritance method parent class (base class)
benefits: reduce duplication of code
insert image description here

insert image description here

read file ifstream/fstream

The first three are read by line, the fourth is the lowest efficiency of reading a single character, the first two are character arrays in c language to represent a string, and the third is C++string to represent a string.
insert image description here

The difference between resize and reserve in vector

1. Capacity is assigned when the container is initialized, which refers to the maximum number of elements that the container can hold. At this time, no object has been created in the container and cannot be accessed through subscripts.
2. size refers to the actual number of elements in the container, which can be accessed by subscript.
resize both allocates memory space and creates objects. That is, modifying the capacity size also modifies the size size.
reserve only modifies the capacity, not the size.

smart pointer

shared_ptr, unique_ptr, weak_ptr
shared_ptr: The reference count is stored on the heap, allowing multiple pointers to point to the same object
unique_ptr: Exclusively points to the object, and cannot assign the object pointed to by one unique_ptr to another unique_ptr. No reference counting, better performance than shared_ptr
wear_ptr: It is a smart pointer that does not control the lifetime of the pointed object, pointing to the object managed by shared_ptr.

lvalues ​​and rvalues

Those that can take addresses, have names, and non-temporary ones are lvalues.
The address cannot be taken temporarily without a name, and usually the life cycle is an rvalue within an expression.

quote

The alias of the object, that is, the object itself, modifying the formal parameter will change the actual parameter itself.
Cannot make a reference point back to another object
Use references whenever possible, pointers as a last resort

Macro definition & const difference

(1) The compiler handles it differently

  • The define macro is expanded during the preprocessing phase.
  • const constants are used during compilation and runtime.

(2) Types and security checks are different.
const constants have data types, but macro constants have no data types. The compiler can perform type safety checks on the former. For the latter, only character replacement is performed, without type safety checks, and unexpected errors (marginal effects) may occur in character replacement.

  • The define macro has no type, does not do any type checking, just expands.
  • A const constant has a specific type, and type checking is performed during compilation.

(3) Different storage methods

  • The define macro is only expanded, as many times as it is used, it will not allocate memory.

  • const constants are allocated in memory (either on the heap or on the stack).

(4) const can save space and avoid unnecessary memory allocation.
(5) The compiler usually does not allocate storage space for ordinary const constants, but saves them in the symbol table, which makes it a constant during compilation, without the operation of storing and reading memory, making it very efficient high.

string & char conversion

1. Convert string to char*.

There are three main methods to convert str to char* type, namely: data(); c_str(); copy();
1.data() method, such as:

string str = "hello";
const char* p = str.data();//加const  或者用char * p=(char*)str.data();的形式

2.c_str() method, such as:

1 string str=“world”;
2 const char *p = str.c_str();//同上,要加const或者等号右边用char*

3.copy() method, such as:

1 string str="hmmm";
2 char p[50];
3 str.copy(p, 5, 0);//这里5代表复制几个字符,0代表复制的位置,
4 *(p+5)=‘\0;//注意手动加结束符!!!

2. Convert char * to string.

can be assigned directly.

1 string s;
2 char *p = "hello";//直接赋值
3 s = p;

push_back & emplace_back

The difference between emplace_back() and push_back() lies in the underlying implementation mechanism.
When push_back() adds an element to the end of the container, it will first create the element, and then copy or move the element into the container (if it is a copy, it will destroy the previously created element afterwards);
and emplace_back() in When implemented, the element is created directly at the end of the container, eliminating the need to copy or move the element

Pointer Constant & Constant Pointer

const pointer (pointer): A pointer (to a constant).

int a = 10;
int b = 20;
const int* p1 = NULL;
p1 = &a;
// 指针p1所指向的变量是一个常量,即指针p1在解引用时,值不能改变
p1 = 20;在这里插入代码片

Pointer constant (constant): (the pointer itself is a) constant.

// 指针常量
int const p2 = &a;
// 指针只能指向a的地址,a的值可以改变,指针p2已经指向了a的地址,所以不能指向b;
p2 = &b;

It can also be understood in this way: directly look at the position of the const keyword as follows:
constant pointer:

const int p1 = nullptr;或者 int const * p1 = nullptr;

Pointer constants:

int* const p2 = nullptr;

Guess you like

Origin blog.csdn.net/qq_39506862/article/details/129699029