Introduction to Standard Template Library (STL)

Introduction to Standard Template Library (STL)

In this article, you will be learning about STL the acronym for Standard Template Library.

PREREQUISITE

should have a working knowledge of template classes.

This article is all about the Introduction to Standard Template Library (STL).

As the name suggests the Standard Template Library is a set of C++ template classes to provide common programming data structures and functions like vectors, maps, lists, sets, stacks, arrays, etc.

This library contains all the container classes, algorithms, and iterators. All the functions and methods are generalized in this library, so its components are parameterized. And that makes it easy to use the same container for any data type. For example, the sort algorithm about which we will be learning soon can sort the elements in the given container irrespective of the data type.

As STL provides numerous containers and algorithms which are very useful in competitive programming as they are easy to use and takes significantly very less lines of code for execution. If we take the implementation of a linked list it could be done quite easily with just a single statement by using the list container of the container library in STL.

STL has 4 components:

  • Algorithms
  • Containers
  • Iterators

Algorithms in STL

The header algorithm defines a collection of functions specially designed to be used on the container irrespective of their type. It has complex algorithms that can be performed on different data structures.

The Algorithm library provides abstraction, i.e. you don’t necessarily need to know how the algorithm works, i.e. the internal implementation of the algorithm.

See also  UNORDERED MULTIMAP in Cpp

Following are the algorithms that are available in this algorithms header:

  • Sorting
  • Searching
  • STL Algorithms
  • Array Algorithms
  • Partition Algorithms

Containers in STL

The STL container library provides containers that are used to create data structures like arrays, linked lists, trees, etc. They store objects and data.

There are a total of seven standard “first-class” container classes and three container adaptor classes and only seven header files that provide access to these containers or container adaptors.

  • Sequence Containers: Implements data structures that can be accessed sequentially.
    • vector
    • list
    • deque
    • arrays
    • forward_list (Introduced in C++11)
  • Container Adaptors: provide a different interface for sequential containers.
    • queue
    • priority queue
    • stack
  • Associative Containers: Implements sorted data structures that can be quickly searched in O (log n) complexity.
    • set
    • multiset
    • map
    • multimap
  • Unordered Associative Containers: Implement unordered data structures that can be quickly searched.
    • unordered_set
    • unordered_multiset
    • unordered_map
    • unordered_multimap

Iterators in STL

Iterators in STL are used to point to the containers. They act as a bridge between containers and algorithms.

For example, the sort() algorithm has two parameters, starting iterator and an ending iterator, now sort() compares the elements pointed by each of these iterators and arranges them in sorted order, thus it does not matter what is the type of the container and the same sort() can be used on different types of containers.

The STL is one of the reasons why C++ is used for Competitive Programming.

Conclusion:

That is all for this article we will dive deep into the containers, algorithms, and iterators in the coming article.

See also  Expressions in C++

Happy Coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh