C++ interview knowledge points -------------(1)

C++ interview knowledge points -------------(1)

  • The array name is a pointer constant and cannot be changed
  • Right shift operation positive number high bit fill zero, negative number high bit fill 1

What is the difference between vector's resize and reserve?

In vectorthe size()and capacity()attributes correspond to the resize(size_type)and reserve(size_type)the two methods.

  • size: Indicates the number of elements stored in the actual container
  • capaciy: Indicates how many elements are allowed to be stored before reallocation occurs

resize Calling the resize method on a vector container will be at the end of the containerAdd toordeleteSome elements to make the container reach the specified size

reserveCall the reserve method on a vector container to only set capacitythis value

reserve(size_type) is just a changed capaciyvalue, at this time these memory spaces may still == "wild"of. If using[]Operator to access, there may be an arrayOut of bounds == problem.

If there is a large amount of data that needs to be pushed_back, you should use the reserve() function to set its capacity in advance, otherwise there will be many capacity expansion operations, resulting in low efficiency.

This is a benefit of vector and its capacity() mechanism reduces the number of memory allocations. It is equivalent to saying that resize() also does reserve() for us.

What is the difference between the [] index of vector and at?

Access characters in a string

You can use ==[]orThe at()== method to access the characters in the string, the starting index is 0. The maximum effective index is string.length()-1. (In particular, if it is an object of const string type, then the maximum effective index is string.length(), and the last character is '\0’.)

The difference between [] and at() is that [] does not check whether the index is valid, while at() will throw out_of_range exception when encountering an invalid index .

Can the constructor be a virtual function?

answer: Can't

1) Because the type of the object needs to be determined when creating an object, and the virtual function determines its type at runtime . When constructing an object, because the object has not been created successfully, the compiler cannot know the actual type of the object, whether it is the class itself or a derived class, etc.

2) The call of virtual function needsVirtual function table pointer, And the pointer is stored in the memory space of the object ; if the constructor is declared as a virtual function, then because the object has not been created yet, there is no memory space, and there is no virtual function table address to call the virtual function, that is, the constructor.

Handwritten list structure

* struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };

The key point, at the end there is asemicolon, The structure is also usedsemicolon

Virtual function mechanism

Virtual functions are used to implement dynamic polymorphism;

The declaration of virtual function is to add keywords in front of the function virtual.

The realization of polymorphism is dependent onVirtual function tablewithVirtual table pointer

As long as there is a virtual function in a class, then this class will have a virtual table. The determination of the virtual table is determined during the compilation phase. All objects of the class share a virtual table, and all declarations of virtual functions will increase the overhead.

The address of the virtual function is placed in the virtual table, and the object with the virtual table on the tenth of the object structure will have oneVirtual pointer, This pointer will point to the virtual table. If the derived class inherits the base class, andOverride the virtual function method of the base classThen the derived class virtual table will update the address of the virtual function to the address of its own virtual function. The virtual table pointer belongs to the base class, so when you can call the virtual function, first find the corresponding virtual table through the virtual table pointer, and then call the function to achieveDynamic binding

link

Do you understand the role of const

C++ constallows you to specify a semantic constraint, and the compiler enforces this constraint, allowing the programmer to tell the compiler that a certain value remains unchanged. If a certain value does remain unchanged in programming, it should be used explicitly const, so that the help of the compiler can be obtained.

  • const Modified constant means that you cannot change eg const int a = 5;
  • const Modified pointer variable
A: const 修饰指针指向的内容,则内容为不可变量。   const int * p = 5;
B: const 修饰指针,则指针为不可变量。 		  int * const p = &a
C: const 修饰指针和指针指向的内容,则指针和指针指向的内容都为不可变量。const int * const p = &a
  • const Decorate function parameters and return values
修饰按值或者按引用传递的参数时候,表示不能修改内容
修饰指针时候可以防止指针被篡改
const 修饰内置类型的返回值,修饰与不修饰返回值作用一样。
const 修饰自定义类型的作为返回值,此时返回的值不能作为左值使用,既不能被赋值,也不能被修改。
const 修饰返回的指针或者引用,是否返回一个指向 const 的指针,取决于我们想让用户干什么。
  • const When decorating functions

    const 修饰类成员函数,其目的**是防止成员函数修改被调用对象的值**,如果我们不想修改一个调用对象的值,所有的成员函数都应当声明为 const 成员函数。
    
    注意:**const 关键字不能与 static 关键字同时使用,因为 static 关键字修饰静态成员函数,静态成员函数不含有 this 指针,即不能实例化,const 成员函数必须具体到某一实例。**
    

Guess you like

Origin blog.csdn.net/ahelloyou/article/details/115254126