Use scenarios of vector and deque

  1. Vector usage scenarios: such as the storage of software history operation records, we often need to view the history records; another example, the last record, the last record, but the record will not be deleted, because the record is a description of the fact.
  2. Use scenarios of deque: For example, in the queuing ticket purchase system, deque can be used for the storage of queued people, which supports fast removal of the head end and fast addition of the tail end. If a vector is used, when the head end is removed, a large amount of data will be moved, which is slow.
  3. Comparison of vector and deque:
    3.1 vector.at() is more efficient than deque.at(). For example, vector.at(0) is fixed, but the start position of deque is not fixed.
    3.2 If there are a lot of release operations, vector takes less time, which is related to the internal implementation of the two.
    3.3 Deque supports fast insertion and fast removal of the head, which is the advantage of deque.
  4. Use scenarios of list: 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.
  5. Set usage scenarios: such as the storage of personal score records for games, the storage requirements are arranged in order from high scores to low scores.
  6. Use scenarios of map: For example, storing 100,000 users by ID number, and 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, in the worst case, you may have to traverse the entire container to find the user.
vector and list set impress map mulitmap
Memory structure Single-ended array Double-ended array Doubly linked list Binary tree Binary tree Binary tree Binary tree
Random storage Yes Yes no no no For key, not no
Element search speed slow slow very slow fast fast For key, fast For key, fast
Element insertion and deletion position Tail end Head and tail any position --- --- --- ---

Guess you like

Origin blog.csdn.net/sanqima/article/details/103761280