DEV Community

Cover image for Standard Template Library in C++
Abhishek Chandra
Abhishek Chandra

Posted on • Edited on

Standard Template Library in C++

vector<int> LET_S;
pair<string, int> GET;
list<int> STARTED;
array<int, 3> WITH;
map<int, string> STL;
Enter fullscreen mode Exit fullscreen mode

Still don't know what these statement do?

Let's read this article to head start your journey in STL.

Standard Template Libraries are powerful set of C++ template classes. It contains numerous pre-defined classes and are used to make programming easier.

Consider Standard Template Libraries as a helping hand for not writing codes to implement Data Structures like Linked Lists, Stacks, Queues, Trees etc. in every program we require them (mostly in competitive programming questions), we can use these data structures by just including a library and using a pre-defined syntax.

Also, classes in Standard Template Libraries are made through template classes so these are generic in nature.

At the core of the C++ Standard Template Libraries, these are the following three well structured components.

  • Containers.
  • Algorithms.
  • Iterators.

Containers

  • Containers are used to manage and store collection of objects of a certain kind.
  • Container Library in STL provide containers that are used to create data structure like arrays, linked-list, trees, etc.
  • Containers helps us to implement and replicate simple and complex data structures very easily like array, list, trees, associative arrays and many more.
  • These containers are generic in nature, they can hold elements of any data types.

Common Containers that replicates simple and complex data structures

  • vector : replicates arrays
  • queue : replicates queues
  • stack : replicates stack
  • priority_queue : replicated heaps
  • list : replicates linked list
  • set : replicates trees
  • map : associative arrays

Classification of containers

  • Sequence Containers : These containers hold linear storage of data, like arrays, linked list etc.
  • Associative Containers : Ordered containers like map, multimap, set, multiset are associative containers. (Ordered means the values are stored in the respective container in the same order as inputted by the user)
  • Unordered Associative Containers : These are similar to associative containers but have different constraints, the elements are stored unordered due to the use of hash tables while implementing these containers.

How to use containers?

When we use containers to implement data structures, we just have to include a header file and use the respective syntax to initialise the data structure you need.

#include<bits/stdc++.h>
Enter fullscreen mode Exit fullscreen mode

or you can just include a specific container header file,

#include<iostream>

// importing vector header file to implement vectors
#include<vector>

int main(){
 vector<int> first_vector;
 return 0;
}

// vector can be used for creating dynamic arrays of `char`, `int`, `float` and other types.

Enter fullscreen mode Exit fullscreen mode
#include<iostream>

//  importing list header file to implement lists
#include<list>

int main(){
 list<int> my_list;
 return 0;
}

Enter fullscreen mode Exit fullscreen mode

Algorithms

  • Algorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of container.
  • Algorithms Library contains built-in functions that perform complex algorithms on the data structures.

Example:

  • We can reverse a range with reverse() function, sort a range with sort() function, search on a range with binary_search() and so on.

  • Algorithm Libraries provide abstraction, i.e. you don't necessarily need to know how the algorithm works.

Sorting of a vector

#include<bits/stdc++.h>

using namespace std;

int main(){
 vector<int> second_vector {44,22,66,33,99};

// begin() and end() method returns an iterator to the vector.
 sort(second_vector.begin(), second_vector.end());
 return 0;
}

/* 
Above code will sort the vector to {22,33,44,66,99}.
More about vector will be covered in a separate node.
*/
Enter fullscreen mode Exit fullscreen mode

Iterators

  • Iterators are used to step through the elements of collection of objects. These collections can be containers or subsets of containers.
  • Iterators in Standard Template Libraries are used to point to the containers.
  • Iterators actually acts as a bridge b/w containers and algorithms.

Let's look at the above code in algorithms section for reference to iterators,

sort() algorithm or function have two parameters, starting iterator (vector.begin()) and ending iterator (vector.end()) pointing to first and last element in the vector respectively, now sort() compares the elements pointed by each of these iterators and arrange them in sorted order, thus it does not matter what is the type of the container and same sort can be used on different types of containers.

How to declare an iterator?

Syntax

// This is an iterator of type vector
vector<int> :: iterator itr; 
// itr is the iterator

Enter fullscreen mode Exit fullscreen mode

Example use of iterator:

Printing elements of a vector

#include<bits/stdc++.h>

using namespace std;

int main(){
    vector<int> third_vector {10,20,30,40,50};
    vector<int> :: iterator itr;
    for(itr = third_vector.begin(); itr != third_vector.end(); itr++){
        cout<<*itr<<" ";
    }
    return 0;
}

/*
OUTPUT : 10 20 30 40 50 
*/
Enter fullscreen mode Exit fullscreen mode

Visit this to view some Standard Template Libraries programs.

Top comments (4)

Collapse
 
sandordargo profile image
Sandor Dargo

In production environment never ever use <bits/stdc++.h>. It will blow the size of your software as it includes so many things, plus it's not portable between different compilers.

Collapse
 
abhishekchandra2522k profile image
Abhishek Chandra

True! Thanks for pointing it out. I was explaining STL from competitive programming point of view. 😅

Collapse
 
sandordargo profile image
Sandor Dargo

Ok, that I didn't think about. So competitive programmers don't use clang or msvcc?

Thread Thread
 
abhishekchandra2522k profile image
Abhishek Chandra

Actually, I think most of the competitive programmers code directly on the platforms like hackerrank, geeksforgeeks etc. where bits/stdc++.h is already included in the directives.😅