Skip to content

Latest commit

 

History

History
120 lines (57 loc) · 5.71 KB

Ep1.5-STL-Extra.md

File metadata and controls

120 lines (57 loc) · 5.71 KB

(Optional) Part 1 Episode 1.5/4: C++ STL Extra Resources

The C++ STL can be broadly divided into four parts:

  1. Containers
  2. Functor (Not important)
  3. Iterators
  4. Algorithms

Containers

Containers are used to manage collections of objects of a certain kind. There are several different types of containers like dequeue, list, vector, map etc.

A short overview of all containers:

  1. Vector - Dynamic arrays

  2. String - Essentially a vector of characters with extra string functionality.

  3. List - Doubly linked lists

  4. Dequeue - Double ended Queue

  5. Array - A container that encapsulates fixed size arrays.

  6. Queue - Queue data structure

  7. Stack - Stack Data structure

  8. Priority Queue - A priority queue is a container adaptor that provides constant time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction.

  9. Set - It is an associative container that contains a sorted set of unique objects.

  10. Multiset - Same as set but supports repeated elements

  11. Map - It is a sorted associative container that contains key-value pairs with unique keys.

  12. Multimap - Same as map, while permitting multiple entries with the same key.

  13. Unordered_set - It is an associative container that contains a set of unique objects of type Key. Search, insertion, and removal have average constant-time complexity.

  14. Unordered_multiset - Same as unordered set but supports repeated elements.

  15. Unordered_map - It is an associative container that contains key-value pairs with unique keys. Search, insertion, and removal of elements have average constant-time complexity.

  16. Unordered_multimap - Same as map but supports mapping one key to many values.

It is recommended that you go through the methods present on each of these containers and understand what they achieve and the time complexity of that method.


Extra: Range Based For Loops

A wonderful feature that was introduced in recent versions of C++ is the range based for loop.

For those that come from other programming backgrounds, this would be a metaphor to a For-Each loop.

A range based for loop provides an easy way of iterating over complex containers without having to setup your beginning and ending conditions.

Tutorial on range based for loops

Video Tutorial


Iterators

PS: [ Iterators are not all that common but sometimes you'll have no choice but to use them and that's why this section exists. Example - Deleting a single element from a multiset can be achieved using mostly iterators. ]

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. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location using them.

Tutorial on Iterators

Video Tutorial


Algorithms

The C++ STL comes with some pre-implemented algorithms and it is important for you to at least know that the following ones exist at your disposal:

  1. std::sort - Sorting a container.

  2. std::reverse - Reversing a container.

  3. std::max_element - Returns an iterator to the largest element of a container.

  4. std::min_element - Returns an iterator to the smallest element of a container.

  5. std::binary_search - Binary search in an ordered container to check existence of element.

  6. std::lower_bound - Returns an iterator pointing to the first element in the range that is not less than (i.e. greater or equal to) value.

  7. std::upper_bound - Returns an iterator pointing to the first element in the range that is greater than value.

  8. std::next_permutation - Permutes the range into the next permutation, where the set of all permutations is ordered lexicographically with respect to operator < or comp.

  9. std::prev_permutation - Transforms the range into the previous permutation from the set of all permutations that are lexicographically ordered with respect to operator < or comp.

Video Tutorial on Lower and Upper bound