Interview Reply of Wuhan excellence technology

Wuhan excellence technology, eight part essay for interview

1. In a 32-bit system, a pointer occupies 4 bytes?
2. Is vector safe in a multithreaded environment? The answer is safe. Multithreading – vector is thread safe, method synchronization, and the timeliness of multithreading is not high.
3. What is the difference between vector resize and reverse?

C + + reference URL

std::vector::reverse(size_type n);
requirement vector The capacity of the container shall be at least sufficient to contain n Elements.
If n greater than vector This method will cause the container to reallocate storage and increase the capacity to the current capacity n Or greater than n. 
If n If the current capacity is not large, the function call will not cause memory reallocation, and the capacity will not be affected.
This function does not affect the container size And cannot change the elements of the container.

#include <iostream>
#include <vector>
using namespace std;

int main(){
	vector<int>::size_type sz;
	vecctor<int> foo;
	sz = foo.capacity();
	cout << "making foo grow:\n";
	for(int i = 0; i < 100; i++){
		foo.push_bacck(i);
		/*
		First, when defining a container, the capacity of the container is 0
		i = 0 capacity changed: 1
		In push_ After back (0), the capacity becomes 1, which is different from the original one, so the output capacity changed: 1
		i = 1 capacity changed: 2
		In push_ After back (1), the capacity becomes 2, which is different from the original one, so the output capacity changed: 2
		i = 2 capacity changed: 4
		In push_ After back (2), the capacity changes to 4, which is different from the original one, so the output capacity changed: 4
		In push_ After back (3), the capacity is still 4
		i = 4 capacity changed: 8
		In push_ After back (4), the capacity changes to 8, which is different from the original one, so the output capacity changed: 8
		In push_ After back (5), the capacity is still 8
		In push_ After back (6), the capacity is still 8
		In push_ After back (7), the capacity is still 8
		i = 8 capacity changed: 16
		In push_ After back (8), the capacity changes to 16, which is different from the original, so the output capacity changed: 16
		In push_ After back (9), the capacity is still 16
		...
		i = 16 capacity changed: 32
		In push_ After back (16), the capacity changes to 32, which is different from the original, so the output capacity changed: 32
		...
		i = 32 capacity changed: 64
		In push_ After back (32), the capacity changes to 64, which is different from the original one, so the output capacity changed: 64
		i = 64 capacity changed: 128
		In push_ After back (64), the capacity changes to 128, which is different from the original, so the output capacity changed: 128
		*/
		if(sz != foo.capacity()){
			sz = foo.capacity();
			cout << "capacity changed: " << sz << endl;
		}
	} 
}

According to the example given on the official website, there seems to be no difference between reverse and resize.
However, we can see from the above that when the capacity is insufficient, it will be automatically expanded.
Refer to the other two Blogs:
The difference between resize() and reserve() in vector
Difference between resize() and reserve() in C++ vector
explain:
The first is the usage of resize:

_CONSTEXPR20_CONTAINER void resize(_CRT_GUARDOVERFLOW const size_type _Newsize) {
        // trim or append value-initialized elements, provide strong guarantee
        _Resize(_Newsize, _Value_init_tag{});
    }
_CONSTEXPR20_CONTAINER void resize(_CRT_GUARDOVERFLOW const size_type _Newsize, const _Ty& _Val) {
        // trim or append copies of _Val, provide strong guarantee
        _Resize(_Newsize, _Val);
    }

Simply resize(int newsize) and resize(int newsize, type value).
The former is to readjust the size to new size. If the new size is less than oldsize, the extra size will be deleted. If the new size is greater than oldsize, allocate new memory and initialize to the default value.
The difference between resize(int newsize, type value) and resize(int newsize) is that the newly allocated size is initialized with value.

reserve is closely related to the capacity of the container. The source code is:

_CONSTEXPR20_CONTAINER void reserve(_CRT_GUARDOVERFLOW const size_type _Newcapacity) {
        // increase capacity to _Newcapacity (without geometric growth), provide strong guarantee
        if (_Newcapacity > capacity()) { // something to do (reserve() never shrinks)
            if (_Newcapacity > max_size()) {
                _Xlength();
            }

            _Reallocate_exactly(_Newcapacity);
        }
    }

reserve is used to adjust capacity. If newcapacity is greater than oldcapacity, adjust to newcapacity; Otherwise, the capacity will not be adjusted.

difference:
(1) reserve() avoids unnecessary capacity expansion for many times;
(2) resize() changes the size of the container and creates an object;
(3) reserve() changes capacity, but does not change size(); resize() modifies not only size but also capacity.
(4) Because resize() creates an object, all spaces will be initialized, so they can be accessed directly; reserve() does not create an object, so the newly allocated space cannot be accessed directly.

4. auto in C++11
auto is a new keyword in C++11. What is keyword? Just like int, float and double.
The automatic derivation of types is carried out through auto to simplify the programming work.
auto is actually a type derivation at compile time, which has little impact on the running efficiency of the program.
Limitations of auto:
(1) Auto must be an expression, that is, type derivation can only be carried out when auto must be together with the equal sign;
(2) auto cannot be used as a parameter of a function;
(3) In a class or struct, auto cannot be used to give static member variables. This involves compilation related content again;
(4) auto cannot be used for arrays;
(5) auto cannot be used to derive template parameters.

auto a;//error

5,for_ Can each variable in each be changed?
Can be changed. for_each is used as the parameter of a function for each passed in variable. This function can change the variable.

6. How to modify the length of a string?

string s;
s.resize();
void resize(_CRT_GUARDOVERFLOW const size_type _Newsize, const _Elem _Ch = _Elem())

Similar to reserve, if it exceeds, it will be initialized with the default value, and if it does not exceed, it will be truncated.

7. Given the coordinates of a point, how to judge whether the point is on the left or right of the line?
(1) If the line is perpendicular to the x axis, directly judge the x coordinates of the two; If the line is horizontal, judge y;
(2) If the line is tilted, then substituting x into it will get y. just compare y on the line with y at the point.

8. Singleton mode and factory mode
Cheng Jie's "Dahua design pattern" has it, which is very easy to understand.
Supplement from 9:
The singleton mode has three main points:
(1) A class can only have one instance
(2) It must create this instance itself

public class GirlFriend{
	private static GirlFriend girlFriend;
	//private volatile static GirlFriend girlFriend;
	private String name;

	private GirlFriend(String name){
		this.name = name;
		System.out.println("Production completed");
	}
	public static GirlFriend getInstance(String name){
		if(girlFriend == null){
			girlFriend = new GirlFriend(name);//It must create this instance itself
			//It must provide this instance to the entire system itself
		}
		return girlFriend;
	}
}

(3) It must provide this instance to the entire system itself

9. Are there two lazy and hungry modes in the singleton mode?
I have read an interesting article: Singleton mode – my robot girlfriend
Written in java, volatile and synchronized are not well understood.

10. In the SQL server database, find the first 20 IDS, which are not connected.
Give up now.

11. The linked list implements stack operation:
Data structure: stack ADT: use linked list to realize the basic operation of stack.
First, the linked list has a header stage. In addition to the header node, it is the location of the element.
Each time the stack is loaded, a new node is inserted at the next position of the head node;
Each time the stack is released, a node is free at the next position of the header node.

12. Mutual exclusion between threads
Semaphores and mutexes

13. Inter thread communication mode?

Thread mutex: mutex lock, critical resource, which can only be accessed by one thread at a time.
Thread synchronization (Producer / consumer issues)
Thread communication: threads share the memory address space of the same process and realize data exchange through global variables.

Tags: C++ Interview

Posted on Fri, 01 Oct 2021 19:59:55 -0400 by xplore