Standard Template Library Flashcards
What kind of iterators are available?
- Input
- Output
- Forward
- Bidirectional
- Random access
What is a priority queue?
Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non decreasing order
Are keys in unordered sets unique?
Unordered sets are containers that store unique elements
What is a heap?
What can it retrieve?
Heap data structure can be implemented in a range using STL which allows faster input into heap and retrieval of a number always results in the largest number i.e. largest number of the remaining numbers is popped out each time.
Unordered Maps. How are the elements organized in memory?
They are not organized based on the key, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values
What can I add to a map?
{key, value}
Do list provide random access?
No, since they are not contiguous in memory
What is the time complexity of insert to priority queue?
Logarithmic
What is a bidirectional iterator?
They can move in both directions (forward and backward)
How much time does it take to remove the last element in a vector?
Removing the last element takes only constant time because no resizing happens.
What is the time complexity of access to priority queue?
Constant O(1) to largest element
What is a pair? How do I create one?
What is contained in a pair?
The pair container is a simple container defined in header consisting of two data elements or objects.
The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).
Pair is used to combine together two values which may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.
How can I move an iterator forward or backward by a given amount?
advance(pointer, quantity). Quantity can be positive or negative
What is the time complexity of insert into stack?
Inserting into a stack can only happen at the top, so insertion is O(1).
What is the erase time complexity of vector?
Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.
How can I sum all the elements in a container?
accumulate(first_iterator, last_iterator, initial value of sum)
What is the access time complexity of list?
O(1) if you already have the iterator to the element, O(N) if not
Where can I add elements to a vector?
In the back, with push_back()
Are key values unique in a map?
Yes, a map has unique keys
Are lists faster or slower to traverse than vectors?
As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick.
How can I implement a deque?
1) Doubly-linked list, allowing add/remove from both ends.
2) Dynamic array
What is the time complexity of access in a map?
Logarithmic
What is the time complexity of erasing from stack?
Deleting from a stack only happens at the to, so deletion is O(1).
What is the time complexity of access to queue?
O(N)
What is a deque? What is the policy?
Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.
How are elements internally sorted in unordered set?
Internally, the elements in the unordered_set are not sorted in any particular order, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their values
What is un unordered set?
Unordered sets are containers that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value. It is basically a hash set.
What is the time complexity of finding in stack?
Searching for a given value in the stack requires repeatedly popping elements off the top of the stack until you find the value you seek. So search is O(n)
What is the syntax to create a map?
std::map mapOfWords;
What the STL?
Which are the main components of the STL?
The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms and iterators. STL has the following components - Algorithms - Containers - Functions - Iterators
Is an unordered map faster than a map in retrieval?
unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.
What is an iterator?
How can I move through the content of a container?
An iterator is an object (like a pointer) that points to an element inside the container.
We can use iterators to move through the contents of the container.
What is the time complexity of erase to deque?
Constant O(1) at begin/end, linear O(n) in the middle
Are values unique in a map?
No, a map allows multiple elements to have the same value, as long as they have different keys.
What is a stack?
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.
What is the time complexity of erase in a map?
Logarithmic
Is there a sorting algorithm in the STL?
What is the std::sort() computational complexity?
- Yes, std::sort()
- O(N * log(N)) since it is quick sort
What is the time complexity of erase to queue?
O(1)
What does make_pair() do?
It allows to create a value pair without writing the types explicitly.
pair my_pair = make_pair(0, 2.0);
What is a constant iterator?
const_iterators don’t allow you to change the values that they point to
What’s the syntax of bsearch()? What does it return?
The syntax is std::binary_search(startaddress, endaddress, valuetofind). It returns true if the element is found, false otherwise.
heap::make_heap()
This function is used to convert a range in a container to a heap.
How can I know how many times an element is in a container?
count(first_iterator, last_iterator,x) – To count the occurrences of x in vector.