A Guide To STL

Shivani Sundriyal
4 min readJan 7, 2021

We will discuss the following topics in this blog:

  • STL introduction
  • Components of STL
  • Containers
  • Algorithm
  • Iterators

Introduction:

Every time you felt the need for a stack or a map, you’ll pull up your sleeves and start writing the redundant classes all over again?
Well, not more!

Yes…Of course! We can implement it by using Standard Template Library.

Standard Template Library is all about data structures and algorithms which are already implemented in C++ internal library and we can use them as programmers of C++.They are predefined and pre implemented.

STL being generic library provide containers and algorithms which can be used to store and manipulate different types of data thus saves us from defining these data structures and algorithms from the scratch.

Components of STL:

STL has three components

o Containers

o Algorithms

o Iterators

1. Containers:

  • An object that stores data in memory into an organized fashion.
  • The containers in STL are implemented by template classes and therefore can be easily modified and customized to hold different types of data.

2. Procedure (Algorithms)

  • Algorithms are used to process the data contained in the containers .
  • The STL includes many different kinds of algorithms to provide support to tasks such as initializing, searching, copying, sorting, and merging, copying, sorting, and merging.
  • Algorithms are implemented by template functions.

3. Iterator

  • It can be defined as an object that points to an element in a container.
  • Iterators are handled just like pointers.
  • Iterators connect algorithm with containers and play a key role in the manipulation of data stored in the containers.

Containers:

There are ten containers which can be grouped under three categories.

Let’s discuss each one of them briefly

Container : Vector

  • Description: It can be defined as a dynamic array. It permits direct access to any element.
  • Header File: <vector>
  • Iterator: Random access

Container : List

  • Description: It is a bidirectional linear list. It allows insertion and deletion anywhere
  • Header File: <list>
  • Iterator: Bidirectional

Container : Deque

  • Description: It is a double-ended queue. Allows insertions and deletions at both the ends. Permits direct access to any element.
  • Header File: <deque>
  • Iterator: Random access

Container : Set

  • Description: It is an associate container for storing unique sets. Allows rapid lookup.
  • Header File: <set>
  • Iterator: Bidirectional

Container : Multiset

  • Description: It is an associate container for storing non-unique sets.
  • Header File: <set>
  • Iterator: Bidirectional

Container : Map

  • Description: It is an associate container for storing unique key/value pairs. Each key is associated with only one value.
  • Header File: <map>
  • Iterator: Bidirectional

Container : Multimap

  • Description: It is an associate container for storing key/value in which one key may be associated with more than one value (one-to-many mapping). It allows a key-based lookup.
  • Header File: <map>
  • Iterator: Bidirectional

Container : Stack

  • Description: A standard stack follows last-in-first-out(LIFO)
  • Header File: <stack>
  • Iterator: No iterator

Container : Queue

  • Description: A standard queue follows first-in-first-out(FIFO)
  • Header File: <queue>
  • Iterator: No iterator

Container : Priority-Queue

  • Description: The first element out is always the highest priority element
  • Header File: <queue>
  • Iterator: No iterator

Now, Let’s understand the difference between three categories of Containers

Sequence Containers:

· Sequence containers store elements in a linear order.

· All elements are related to each other by their position along the line.

· They allow insertion of element and all of them support several operations on them.

· Example: Vector, List and Deque.

Associative Containers:

· They are designed in such a way that they can support direct access to elements using keys.

· They are not sequential.

· They store data in tree which facilitates fast searching, deletion, and insertion unlike sequential.

· Example: Set ,Multiset, Map and Multimap

Derived Containers:

· These are also known as container adaptors.

· The derived containers do not support iterators and therefore we cannot use them for data manipulation.

· They support two member function pop() and push() for implementing deleting and inserting operations.

· Example: Stack, Queue and Priority Queue.

Algorithm:

  • They are functions that can be used generally across a variety of containers for processing their content.
  • Standard algorithms permit us to work with two different types of containers at the same time.
  • It saves a lot of time and effort of programmers
  • #<algorithm> is the header file used to access STL algorithms.

Iterators:

  • Iterators connect containers with algorithms and play a vital role in the manipulation of data stored in the containers.
  • They are often used to pass through from one element to another, this process is called iterating through the container.
  • There are five types of iterators:

· Input

· Output

· Forward

· Bidirectional

· Random

Thank you for reading!

--

--