Posts

Showing posts from April, 2025

C++ data structure and containers and their operation with time complexity

C++ provides a rich set of data structures and containers in its Standard Library, particularly within the <vector>, <list>, <deque>, <set>, <map>, <unordered_set>, and <unordered_map> headers. Here is a summary of some of the key data structures, their operations, and the associated time complexities: Vector (`std::vector`) - Access: O(1) for random access using `operator[]` or `at()`. - Insertion/Deletion at End: Amortized O(1) for `push_back()`, O(1) for `pop_back()`. - Insertion/Deletion at Beginning or Middle: O(n). - Search: O(n) for linear search. List (`std::list`) - Access: O(n) for random access (lists do not support direct access). - Insertion/Deletion at Beginning or Middle: O(1) (constant time since it is a doubly linked list). - Search: O(n) for linear search. Deque (`std::deque`) - Access: O(1) for random access using `operator[]` or `at()`. - Insertion/Deletion at End: O(1) for `push_back()` and `pop_back()`. - Inserti...