My Blog List

A brief summary over STL Container

Here is a brief summary talking about common STL Container used in Leetcode problems.


  1. Sequence Containers:
    1. Vector. 
      1. Just like the array in c language that random access runs in constant time. 
      2. Implemented by the dynamic array ensuring that the amortized time of push_back and pop_back is constant time. 
      3. Modify elements other then the last element run in O(n) time 
    2. Deque.
      1. Random access runs in constant time as well
      2. Implemented by double dynamic array I guess thus can push and pop elements both at the end and the front in constant time
      3. Modify elements other than the first and last elements run in O(n) time.
    3. List
      1. Like linked list in c language that random access will runs in O(n) time
      2. Obviously that insert and delete element run in constant time
  2. Container Adapters:
    1. Stack and Queue.
    2. Priority Queue
  3. Associative Containers:
    1. Set.
      1. Implemented by binary search tree (not hash table), thus the time complexity of insert and erase are O(logn)
    2. Map
      1. The same method with set thus [ ]  operator is O(logn)
    3. Unordered_Set and Unordered_Map
      1. New in C++11 that insert erase and [] operators run in O(1) or O(n)
      2. Implemented by hash table
      3. The advantage of set over unordered_set is set requires limited memory in proportion to the input size
Sequnce Containers can be used in Sort function directly with customized compare function.



No comments:

Post a Comment

Enter you comment