Introduction to C++ STL

STL stands for “Standard Template Library”. It is a set of C++ templates which provide some most significantly used data structures and functions while programming in C++.

The classes and functions provided by C++ STL are generic in nature i.e. we can customize these by providing data types to their definitions as parameters.

Templates in C++

There are two kinds of templates in C++:

1. Function Template

It is one of the very useful tool provided by C++ STL using which we can define more generalized functions in C++.

Example: If we have to make a function in C++ which can multiply any two numbers i.e. it can be of any type int, float, double, etc.

There is no need to write different C++ code for the same function to handle numbers of different data types.

#include <bits/stdc++.h>

using namespace std;

template <typename T>

// defining function templete

T mul(T a, T b)
{
    return a*b;
}

int main()
{
    cout<<mul(5,3)<<endl;
    cout<<mul(2.5,1.5)<<endl;
    return 0;
}

Output:

15
3.75

2. Class Template

It is another useful tool provided by C++ STL using which we can define more generalized classes in C++.

#include <bits/stdc++.h>

using namespace std;
template <typename T1, typename T2, typename T3>

// defining class templete
class triplet
{
    T1 a;
    T2 b;
    T3 c;
    public:
    triplet(T1 x, T2 y, T3 z)
    {
        a = x;
        b = y;
        c = z;
    }
    void get_elments()
    {
        cout<<a<<endl;
        cout<<b<<endl;
        cout<<c<<endl;
        cout<<endl;
    }
};

int main()
{
    triplet <float, float, float> t1(1.77, 2.55, 7.66);
    t1.get_elments();

    triplet <int, char, float> t2(7, 'A', 7.66);
    t2.get_elments();

    return 0;
}

Output:

1.77
2.55
7.66

7
A
7.66

Components of C++ STL

1. Containers

In C++ STL containers are used to create data structures like arrays, linked lists, trees, etc. These containers are generalized i.e. they can hold elements of any data types.

Example: In C++ vector is a container used to create dynamic arrays of any data type elements like int, char, float.

Containers in C++ is further categorized into:

  1. Sequence Containers or (Simple Containers): E.g. vector, pair, list
  2. Adapter Containers: E.g. stack, queue
  3. Associative Containers: E.g. set, map

Associative Containers are further categorized into:

  1. Ordered Associative Containers: E.g. set, multiset, map, multimap
  2. Unordered Associative Containers: E.g. unordered_set, unordered_multiset, unordered_multimap

Also read Basic Operations on Matrices in C++

The two very basic difference between Ordered & Unordered Associative containers are:

  • The time complexity of operations like insertion, deletion, searching in the case of unordered associative containers is O(1), while in the case of ordered associative containers is O(logN).
  • The output of unordered associative containers is not sorted but the output of ordered associative containers is sorted.

2. Algorithms

C++ STL provides a set of algorithms which can be implemented on any container irrespective of their type. C++ STL contains some built in functions which implement complex algorithms on the data structures.

For example:

reverse(): It reverses a range.

sort(): It sorts a range.

binary_search(): It searches in a range.

3. Iterators

C++ STL provides a set of iterators which are used to point specific locations of containers. It acts as a bridge between containers and algorithms.

Iterators return a pointer to a specific location within the container.

For example:

begin(): It is a starting iterator that returns a pointer to the starting location of the container.

end(): It is a ending iterator that returns a pointer to the ending location of the container.

Also Read Types of Looping statements in C/C++

Advantages of using C++ STL

  • It provides generic containers and algorithms which extend the functionality of the C++ program/code.
  • It saves a lot of time and effort while coding in C++ as we don’t have to define the required data structures and algorithms from scratch.
  • It is reliable and fast.
  • It is a must thing for competitive programming/coding.

Related posts

Five Ways to Calculate Power in C++

File Permissions in Linux

Implementing Stacks and Queues