C/c ++ interview Summary (3) Interview Summary

Source: Internet
Author: User

C/c ++ interview Summary (3) Interview Summary

7. Differences between vector and list (this is also frequently asked)

Like arrays, a vector has a continuous memory space and the starting address remains unchanged. This is very efficient for random reads (that is, all our [] operators ), because the memory is continuous. If we want to insert or delete elements, we need to copy and move the current elements. If the objects stored in the vector are large, or the constructor is complicated, the overhead of copying existing objects is very high (the copy constructor must be called to copy objects ), each time the vector expands its capacity, it doubles the capacity. (because the elements in the vector are stored consecutively, you cannot find a place to store them. Therefore, the vector will allocate a large memory, copy the original data and release the original space, which is generally larger than the memory required for storing the data, in this way, when there are other elements that need to be stored, you do not need to open up the memory ).

List objects are stored discretely (that is, the memory is not continuous). to randomly access an element, You need to traverse the list, but the efficiency of inserting elements is very high (you only need to change the element pointer, insert at the beginning and end is the most efficient-For details, refer to the operations on the linked list ).

Applicable to vector: the number of objects changes little, simple objects, and frequent Random Access to elements

Applicable to list: large changes in the number of objects, complex objects, and frequent insertion and Deletion

8. The difference between the resize operation of vector and the rserver operation (although I have used it before, I don't know why)
Reserve increases the vector capacity, but its size has not changed!
Resize changes the vector capacity and increases its size!
For more in-depth understanding, you can use Baidu !!

9. Implementation Mechanism of unordered_map and map, performance difference (c ++ may ask when interviewing STL)
In terms of operation efficiency: unordered_map is the highest, and hash_map is the second, while the list with the lowest map efficiency provides ordered sequences.
Memory usage: hash_map occupies the lowest memory, while unordered_map occupies the highest memory.
The difference between unordered_map and stl: map is that stl: map checks whether the elements are the same according to operator <comparison, and compares the element size, select a suitable position and insert it to the tree. Therefore, if the map is traversed (in the middle order), the output result is ordered. The order is sorted by the operator <defined size.
Unordered_map is used to calculate the Hash value of an element. It determines whether the elements are the same Based on the Hash value. Therefore, the unordered_map traversal results are unordered.
The difference in usage is that the key of stl: map needs to define operator <. While unordered_map needs to define the hash_value function and overload operator = (you must customize operator = and hash_value. The heavy-load operator = is because if the hash_value values of the two elements are the same, it cannot be determined that the two elements are the same. operator = must be called. Of course, if the hash_value value is different, you do not need to call operator = ).
Do not worry about built-in types, such as string.
For custom type keys, you need to reload operator <or hash_value () by yourself.
It is best to use unordered_map when no sorting of results is required.
Implementation Mechanism:
The internal implementation of map is the binary balancing tree (I do not understand the concept of the red and black trees, but I still need to work hard)
The implementation of unordered_map is hash_table;
Hash_map is implemented before unordered_map is implemented, but unordered_map is added as the STL standard. hash_map is incompatible with c ++ stl APIs, and c ++ tr1 (C ++ Technical Report1) as a standard extension, hash map is implemented and an api that is compatible with stl is provided, called unorder_map. in the header file <tr1/unordered_map>.
Use unordered_map instead of hash_map.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.