stl::(9)Container usage summary

vector and list set multiset map multimap
Classic memory structure Single-ended array Double-ended array Doubly linked list Binary tree Binary tree Binary tree Binary tree
Random access Yes Yes no no no For key: No no
Element search speed slow slow very slow fast fast For key: fast For key: fast
Element placement and removal Tail end Head and tail any position ... ... ... ...
  • Vector usage scenarios : For example, software history operation records, we often want to view the history records, but we will not delete the records, because the records are descriptions of facts.
    Comparison of vector and deque:
    1: vector.at() is more efficient than deque(). For example, vector.at(0) is fixed, and the starting position of deque is not fixed.
    Two: If there are a large number of release operations, vector takes less time, which is related to the internal implementation of the two.
    Three: deque supports fast insertion and removal of the head, which is the advantage of deque.

  • List usage scenarios : For example, in the storage of bus passengers, passengers may get off at any time, supporting frequent removal and insertion of elements with uncertain locations.

  • Set usage scenarios: such as the storage of personal score records for mobile games, and the storage requirements are sorted from high scores to low scores.

  • Map usage scenario: For example, if you want to store 100,000 users by ID number, you want to quickly find the corresponding user by ID. The search efficiency of the binary tree is reflected at this time. If it is a vector container, it may be traversed in the worst case. Only the container can find the user.

Guess you like

Origin blog.csdn.net/qq_40329851/article/details/114381355