Summary of C++ interview knowledge points (1)

(1) New features of C++11

1.nullptr

The purpose of nullptr is to replace NULL.

In a sense, traditional C++ will treat NULL and 0 as the same thing, depending on how the compiler defines NULL, some compilers will define NULL as ((void*)0), and some will directly define it as It is defined as 0.

C++ does not allow direct implicit conversion of void * to other types, but if NULL is defined as ((void*)0), then when compiling char *ch = NULL;, NULL has to be defined as 0. And this will still cause problems, which will lead to confusion in the overloading feature in C++.

In order to solve this problem, C++11 introduces the nullptr keyword, which is specially used to distinguish between null pointers and 0. The type of nullptr is nullptr_t, which can be implicitly converted to any pointer or member pointer type, and can also be compared with them for equality or inequality. When you need to use NULL, use nullptr directly.

2. Type deduction auto/decltype

C++11 introduces the two keywords auto and decltype to implement type deduction, allowing the compiler to determine the type of the variable.

auto str1="aaaaa";//str1的类型是const char*
auto str2="aaaaa"s;//str2的类型是string

Trailing return type, auto and decltype cooperation (C++11):

template<typename T, typename U>
auto add(T x, U y) -> decltype(x+y) {
    return x+y;
}

Starting from C++14, it is possible to directly allow ordinary functions to have return value deduction:

template<typename T, typename U>
auto add(T x, U y) {
    return x+y;
}

3. Interval iteration

std::vector<int> arr(5, 100);
for(std::vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) {
    std::cout << *i << std::endl;
}

become:

// & 启用了引用
for(auto &i : arr) {    
    std::cout << i << std::endl;
}

4. More: https://blog.csdn.net/jiange_zh/article/details/79356417

(2) Judging the type of data in C++

1. typeid

typeid(str)==typeid(int/const char*/string...)`

2. cin

int a;
cin>>a;
if(cin.good()){}else{} 

If you make a mistake and need to re-enter, you need to use the cin.clear() function to clear the error bit.

(3) How are virtual functions implemented in C++?

Realized by virtual function table and virtual function pointer, each object has a virtual function table pointer, and each class has a virtual function table. The virtual function table pointer has a size of 4 bytes and is stored at the start address of the object. All virtual functions of the corresponding class are stored in the virtual function table.

(4) How to get virtual function address

If there is a virtual function in a class, then the compiler will generate a virtual function table for this class. The address of each virtual function is stored in the virtual function table in the order of declaration of each virtual function. It should be noted that this virtual function The function table does not exist in the class, and for each object of this class, the compiler will generate a transparent and invisible pointer for it. This pointer is the virtual function table pointer, which is located at the beginning of the object's memory and points to the The location of the virtual function table. In other words, if there is a virtual function in a class, assuming an object a is declared, in the case of 32-bit compilation, the address of the virtual function table is stored in the first 4 bytes of the memory layout of object a. Therefore, the virtual function table can be found through the first 4 bytes, and in the virtual function table, the addresses of each virtual function are stored in the order of declaration of the virtual functions.

(5) How much space does a 64-bit system store an address

内存地址是16进制保存的,一个内存(内存空间)是一个字节(8bit):
16位操作系统的内存地址占用大小是16位,即2字节
32位操作系统的内存地址占用大小是32位,即4字节
64位操作系统的内存地址占用大小是64位,即8字节
128位操作系统的内存地址占用大小是128位,即16字节

(6) What is the process of visiting Baidu website?

1. DNS resolution

The browser first searches the DNS records cached by the browser itself; if the required records are not found in the browser cache or the records have expired, then the hosts file is searched; if the required records are not found in the hosts file or the records have expired, the domain name The resolution server sends a resolution request; if the domain name resolution server does not have a record of the domain name, it starts recursive + iterative resolution.
(First, our domain name resolution server will send a request to the root domain server, then the root domain server will tell us the IP of the com domain server, and then our domain name resolution server will send a request to the com domain server. The root domain server does not have www. The IP of baidu.com, but it has the IP of the baidu.com domain server. Our domain name resolution server will send a request to the baidu.com domain server. Repeat this until the IP address of www.baidu.com is obtained.)

2. Initiate a TCP request

The browser will select a local port greater than 1024 to initiate a TCP connection request to port 80 of the target IP address. After the standard TCP handshake process, a TCP connection is established.

3. Initiate an HTTP request

In the established TCP connection, a request for a web page is sent according to the HTTP protocol standard.

4. Load balancing

(1) What is load balancing? When a server cannot support a large number of user visits, the method of distributing users to two or more servers is called load balancing.
(2) There are many methods of load balancing, such as Nginx load balancing, LVS-NAT, LVS-DR, etc. Here, we take simple Nginx load balancing as an example.
(3) What is Nginx? Nginx is a performance-oriented HTTP server. Compared with Apache and lighttpd, it has the advantages of less memory and higher stability. Nginx has 4 types of modules: core, handlers, filters, load-balancers. We discuss two of them here, namely the load-balancers module responsible for load balancing and the filters module responsible for performing a series of filtering operations.

(If our platform is equipped with load balancing, the IP address obtained by DNS resolution in the previous step should be the IP address of our Nginx load balancing server. Therefore, our browser sends our web page request to the Nginx load balancing server. Nginx According to the allocation algorithm and rules we set, select a back-end real web server, establish a TCP connection with it, and forward the web page request sent by our browser. The web server receives the request, generates a response, and sends the web page to Nginx load balancing server. The Nginx load balancing server passes the web page to the filters chain for processing, and then sends it back to our browser.)

5. Browser rendering

(1) The browser generates a DOM Tree according to the content of the page. Generate CSS Rule Tree (rule tree) according to CSS content. Call the JS execution engine to execute JS code.
(2) Generate Render Tree (presentation tree) according to DOM Tree and CSS Rule Tree.
(3) Render the web page according to the Render Tree.
When the browser parses the content of the page, it is found that the page references other static content such as images, css files, and js files that have not been loaded, so there is a sixth step.

6. Web page static resource loading

The next process is that the browser loads the picture content under the url according to the url. Essentially, the browser restarts the first part of the process. The difference is that the server responsible for balancing the backend of the server is no longer an application server, but a server that provides static resources.

(7) How to judge the intersection of the linked list? How to judge that the linked list has a ring? How to find the address of the ring entry? Why are you looking for this? how to prove?

Judging linked list intersection

  1. Determine whether the end nodes of the two linked lists are the same node, as long as the last node of the two linked lists is the same node, then the linked lists must intersect.
  2. First obtain the lengths lenListA and lenListB of the two linked lists respectively, and then let the longer linked list take N steps, N=(lenListA-lenListB) absolute value. Then the two linked lists go at the same time until the nodes of the two linked lists are the same. At this time, this node is the starting node where the two linked lists intersect.

Judging that the linked list has a loop (fast and slow pointer

  1. First create two pointers 1 and 2, pointing to the head node of this linked list at the same time.
  2. Start a large loop. In the loop body, let pointer 1 move down one node at a time, let pointer 2 move down two nodes at a time, and then compare whether the nodes pointed by the two pointers are the same. If they are the same, it is determined that the linked list has a cycle, and if they are different, continue to the next cycle.

ring entry address

We first assume that the length of the linked list is N, and the length of the ring is n, then we can directly calculate the meeting nodes of the fast and slow pointers

1. When n>=N/2, the nth node where the fast and slow pointers meet. It can be deduced that the slow pointer must be in the ring at this time.

When the slow pointer walks n steps, the fast pointer walks 2n steps, which is n steps more than the slow pointer, and just goes around the circle and returns to n nodes, so they meet at this time.

2. When n<N/2, the fast and slow pointers meet the (NN%n)th node.

Since N%n<n, the slow pointer is already in the ring at this time, and the fast pointer takes (NN%n) steps more than the slow pointer, and (NN%n) must be a multiple of n, so the fast pointer is only faster than the slow pointer It's just a few full circles around the circle, and the fast and slow hands meet at this time.

(8) How to judge whether two linked lists intersect?

If two linked lists with rings intersect, the rings of the two linked lists must be the same ring.

First judge whether the two linked lists have a ring, and if there is a ring, find the entry point of the ring. Let linked list 1 start from the ring entry point loop1, because loop1 and subsequent nodes are all on the ring, so it will definitely return to loop1 in the future. If the loop entry point loop2 of linked list 2 is not encountered before returning to loop1, then the two linked linked lists do not intersect and return null, otherwise they intersect.

(9) The difference between unordered_map and map

1.头文件 unordered_map>;  map>
2.unordered_map 内部实现:哈希表;map 内部实现:红黑树
3.unordered_map 查找效率:非常高;map 则需要挨个遍历查找,效率低下
4.unordered_map 是否有序:无序;map 内部实现了红黑树,故map存入元素时会自动排序,且默认升序

Guess you like

Origin blog.csdn.net/qq_46140765/article/details/129666900