Monday 19 May 2014

C++ Data Structure I: what is the memory looking like?

1: Vector
Like arrays, vectors use contiguous storage locations for their elements, but unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. It may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time. but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Functions: reserve(), push_back(),capacity(),empty(), operator[], insert()

2: List
Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions. List containers are implemented as doubly-linked lists;
The main design difference between a forward_list container and a list container is that the first keeps internally only a link to the next element, while the latter keeps two links per element: one pointing to the next element and one to the preceding one, allowing efficient iteration in both directions, but consuming additional storage per element and with a slight higher time overhead inserting and removing elements.

Functions: push_front, push_back, pop_back(), insert(), back(), front();

3: Deque
double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).
Both vectors and deques provide a very similar interface and can be used for similar purposes, but internally both work in quite different ways: While vectors use a single array that needs to be occasionally reallocated for growth, the elements of a deque can be scattered in different chunks of storage.

4: Set
Sets are containers that store unique elements following a specific order. The value of the elements in a set cannot be modified once in the container.
Sets are typically implemented as binary search trees.\
Hash: unordered_set

5: Multisets
Multisets are containers that store elements following a specific order, and where multiple elements can have equivalent values.
Multisets are typically implemented as binary search trees.
Hash: unordered_multiset

6: Map
Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.
The mapped values in a map can be accessed directly by their corresponding key using the bracket operator ((operator[]).
Maps are typically implemented as binary search trees.
Function: insert ( std::pair<char,int>('a',100) ); find(), erase();
Hash: unordered_map

7: Multimap
Multimaps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order, and where multiple elements can have equivalent keys.
Hash: unordered_multimap

8: Stack
Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.
By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.

Functions: push(), top(), pop()

No comments:

Post a Comment