STL Associative Containers
How do you access individual elements in a pair?
.first or .second
What is the default load factor for unordered maps?
1
What is a red black subtree?
A self balanced binary search tree with: -Red roots and leaves -Insertion adds a node and colors it red -Longest path to a leaf is no more than twice the shortest path -Guarantees O(log n) lookups and insert/delete
What is a self-balancing search tree?
A tree that maintains its balance after insert/delete operations
What is a binary tree?
A tree where each parent has two children
What is a search tree?
A tree where nodes store keys
What is a sorted binary tree?
A tree where the key in the node is greater than keys in left subtree and less than keys right subtree
What is a balanced search tree?
A tree where there is a constraint on differences in the subtree heights
What is a rooted tree?
A tree with a single distinguished node
What is a subtree?
All the descendants of a node
What is the time complexity of lookup/insertion/deletion for hash tables?
Amortized O(1)
What is a hash table?
An Array of buckets of elements
What is a pair?
An aggregate of values of two, possibly different types
What is an unordered associative container?
An associative container that preserves the order of elements and implemented using search trees
What are pairs used in?
Associative containers
How do maps erase?
At iterator or iterator range, returns iterator to next element past erased element
How does erase work for sets/multisets?
By element, iterator, range
How does insert work for sets/multisets?
By element, with iterator hint, or iterator range
How are maps sorted?
By key
What is an unordered associative container?
Does not order elements and has quicker access using hash tables
What is special about associative container time complexities?
Generally, insertion, deleting, and lookup times are equivalent
What is collision?
Hashing different key to the same bucket and usually resolved through pointer to linked list of elements
What is different between the unordered set and ordered set interfaces?
Nothing
What is different between unordered and orders map interfaces?
Nothing
What does bucket_count() do for unordered maps?
Number of buckets
What is the BigO of map lookup/deletion/insertion
O(log n)
What are the two types of associative containers?
Ordered and unordered
What do maps store?
Sorted key/value pairs
What is a parent node?
The closest ancestor
What is a child node
The closest descendant
What is the height of a tree?
The length of the longest path to a leaf
What is the depth of a root?
The length of the path to the root
What does key erase return on a map?
The number of nodes erased.
How do you declare a map?
map <int, string> employees;
How do you erase by key on a map?
map_name.erase(key1);
How do you range insert on a map?
map_name.insert(container.begin(), container.end());
How do you hint insert with an iterator on a map?
map_name.insert(it, make_pair(val1, val2));
How can you insert with initializer lists on maps if const value?
map_name.insert({val1, val2})
What is index lookup on a map?
map_name[index];
What is a tree?
A graph with no cycles
What is a leaf node?
Node with no children
What is a multi map?
A map that can store several elements with the same key
What is an ancestor node?
A node on path from N to root
What is a descendant node?
A node whose ancestor is N
What is a root node?
A node with no parents
What do you need for maps in a file?
<map> and std::map
How do you use set in a file?
<set> and using std::set or using std::multiset
How do you use unordered maps in a file?
<unordered_map> and using std::unordered_map
How do you use an unordered_set in a file?
<unordered_set> and using std::unordered_set
How do you define pairs in a file?
<utility>
What is an associative container?
A container that does not store elements in a linear order and provides mapping from keys to values.
What is equal_range() for multi maps?
A function that returns a pair of iterators for the range
What are keys implicitly always?
Const
What operations do pairs have?
Copy and comparison
What does bucket(key) do?
Creates a bucket for a key
How can you lookup in a map?
Indexing, find, or range
What does insert() do?
Inserts a pair into map or returns a pair if the key already exists
What happens in hint insert?
Inserts before iterator position or returns iterator to existing pair
What is the different between maps and unordered_map functions?
No lower_bound() or upper_bound() on unordered
Can you modify set keys?
No, must be erased and inserted
Why is overloading index operation on a map unsafe?
It overwrites old key value if it exists or creates a new one if it does not
How do you traverse a map?
Iterating over map by keys or range based for
What does load_factor() do for unordered maps?
Its the size() / bucket_count()
What are sets/multisets?
Keys and values are the same
What is different between a map and multi map interface?
Multimaps do not provide indexing operator
Do sets have indexing?
No
What does erase(element) do for a multiset?
Removes all elements
What does find() do?
Returns iterator at value or returns end() if not found
What does find() do for multi maps?
Returns iterator one of the elements to the key, not necessarily the first
What does upper_bound(key2) do?
Returns iterator to first element greater than key
What does lower_bound(key1) do?
Returns iterator to first element no smaller than key1
What does begin(key), end(key) do?
Returns local_iterator for the chain of elements in a bucket
What does max_load_factor() do for unordered maps?
Returns or sets maximum load factor before bucket count increases
What does the hash function do?
Translates key to bucket index
What happens if keys are equal in range insert?
Unspecified behavior
Are set elements sorted?
Yes
Can maps be const?
Yes
What is the code for iterating over a map?
for (auto it=employees.cbegin(); it != employees.cend(); ++it) { ..... }
What is the code for range based for looping over a map?
for(const auto& e: employees) { .... }
What does insert() return?
pair<map<type1, type2>::iterator, bool> where bool indicates if insert was successful and the iterator is the iterator to the successful insert location or the existing key
How can you construct a pair?
pair<type1, type2>(arg1, arg2)
How can you return a pair of arg1 and arg2?
std::make_pair(arg1, arg2)