What Are STL in C++

By | October 5, 2023

Affordable Business Intelligence software

 It is a container used in the C++ programming language. It can hold objects of the same type inside it. They can help implement different data structures, such as arrays, linked lists, maps, sets, etc. Using STL in C++ saves much time and makes developer’s work easy and productive. In this article, we will learn about the STL in C++. Read the complete article to learn more in detail.

What Are STL In C++

Standard Template Library STL is a robust set of C++ template classes that provide general-purpose classes and functions to implement various commonly used algorithms and data structures. Some of the popular STL in C++ are Vector, pair, stacks, queues, lists, etc. 

With the help of STL in C++, programmers can generally achieve higher productivity while creating readable and maintainable code. It becomes easy to write algorithms and data structures that work with most of the data types. In this article, we will dive deep into the concept and applications of STL In C++. You just need to stay with us and read the complete article to get a better understanding.

Recommended Course 

Components of STL In C++

In STL, there are four major components in C++. Let us have a look at them.

Containers

Containers help in managing collections of objects of certain kinds. Various types of containers are available in the data structure, such as vectors, lists, queues, stacks, sets, maps, etc.

Algorithms

The algorithms are a collection of different functions that help implement various functionalities such as searching, sorting, and others. 

Functions

Functions in STL help provide arguments and operators to tell what operations will be carried out on the elements enclosed. There are several function objects, like predicates and operators, used in functions.

Iterators

Iterators are like pointers in C++ STL. It helps to work in sequence during traversing. It defines the ranges on which the algorithms and functions operate in STL.

Important Containers Of STL In C++

Let us now look at some of the significant STL containers in C++ that are used.

Array

Arrays are sequential data containers of fixed size that store the data in a contiguous memory location. They store multiple values of the same data type sequentially. It is commonly used for storing data, iterating elements, and performing various operations like insertion, deletion, and searching on the elements stored inside the array.

                      array<data type, size>array_name;

Array In STL In C++
array<int, 5> myArray;

Vectors 

Vectors are an essential part of the STL in C++. Unlike arrays, they are like dynamic arrays, which can resize themselves automatically. We can insert or remove elements from a vector easily. They are more flexible and a popular alternative to traditional arrays, allowing us to manage a collection of elements efficiently. Some significant advantages of using a vector are dynamic size, flexibility, random access, automatic resizing, etc. 

                                   vector<data type> vector_name;

 

Vectors STL In C++
#include <iostream>

#include <vector>

using namespace std;

int main() {

    

  vector<int> myVector;

   

    myVector.push_back(1);

    myVector.push_back(2);

    myVector.push_back(3);

  

    cout << “Elements in the vector: “;

    for (int i = 0; i < myVector.size(); ++i) {

            cout << myVector[i] << ” “;

    }

    cout << endl;

    for (const auto& element : myVector) {

       cout << element << ” “;

    }

   cout << std::endl;

    

    myVector.push_back(4);

    myVector.push_back(5);

   

    for (const auto& element : myVector) {

        std::cout << element << ” “;

    }

    std::cout << std::endl;

    return 0;

}

List 

Lists are the container class provided by the STL in C++. It helps to implement doubly-linked lists in data structures. They are not stored at contiguous memory locations like arrays and vectors. In lists, we can perform insertions and deletions from any end. Some essential features of lists are a doubly linked list, dynamic size, efficient insertion and deletion, and no random access. 

                                       

                                      list<data type> list_name

 

List STL in C++
#include <iostream>

#include <list>

Using namespace std;

int main() {

    

    list<int> myList;

    myList.push_back(1); 

    myList.push_front(2); 

    myList.push_back(3);

    myList.push_front(4);

  

    for (const auto& element : myList) {

        cout << element << ” “;

    }

    cout << endl;

  return 0;

}

Unordered Map

The unordered map is a container that stores key-value pairs. It uses hashing to implement faster access times. The keys in the unordered map are unique and are unsorted.

 

unordered_map<data_type, data_type>map_name

 

Unordered Map STL in C++
unordered_map<string,int> freqWords;

string words[]={“apple”, “Banana”, “apple”, “orange”}

 

In the above table, the string containing names of fruits and unordered map freqWords will store the frequency of each fruit with a unique key.

Unordered Set

The unordered set is a container which stores the collection of unique elements inside. It stores the elements by using hashing to provide constant average time complexity for various operations. 

 

                           unordered_set<data type> Name

 

Unordered Set STL in C++
unordered_set<int> mySet;

mySet.insert(1);

mySet.insert(2);

mySet.insert(1);

mySet.insert(3);

mySet.insert(5);

for(const auto & element: mySet){

cout<<element<” “;

}

 

Output
1 2 3 5

 

As we can see the unordered set does not contain the duplicate element 1. It stores only the unique values inside it. It contains various predefined methods like find, insert, erase, etc.

Stack 

 

Stack is a container in the STL in C++ which works on Last In Last Out (LIFO) rule. All the major operations in stack occurs at its top element. It contains various predefined methods such as pop(), push(), peek(), empty(), size(), etc.

                                  stack<data type> Name 4

 

Stack In C++ In STL
#include <stack> 

int main() 

stack<int> numbers;

numbers.push(1);

numbers.push(2);

numbers.push(3); 

int item = numbers.top(); 

numbers.pop();

cout << item << endl; 

return 0; 

}

 

Output
3

 

Queue 

A queue in a C++ in STL is a container which implements a queue interface. It follows the First In First Out (FIFO) principle. It means that the first element which is added to the queue is the element that is first removed.

 

Queue In C++ In STL
#include <queue>

int main() {

 

  queue<int> numbers;

  numbers.push(1);

  numbers.push(2);

  numbers.push(3);

 

  int item = numbers.front();

  numbers.pop();

  cout << item << endl;

  return 0;

}

 

Output
1

 

Queue STL in C++ also contains various predefined methods such as push(), pop(), front(), empty(), size(), etc. They are very useful data structures and are used in implementing various applications such as job queue, buffer data, trees, etc.

Dequeue

A deque is an extension of a queue, it is basically a double-ended queue which allows more faster insertion and deletion from both the end of queue. A queue in which enqueue and dequeue operations can be implemented from both the ends are called dequeue data structures.

 

                                     deque<data type> Name

 

Dequeue STL In C++
#include <deque>

int main() {

  

  deque<int> numbers;

  numbers.push_front(6);

  numbers.push_front(4);

  numbers.push_front(9);

  int item = numbers.front();

  numbers.pop_front();

  cout << item << endl;

  numbers.push_back(4);

  numbers.push_back(5);

  numbers.push_back(6);

  item = numbers.back();

  numbers.pop_back();

  cout << item << endl;

  return 0;

}

 

Output 
9

6

 

Some of the predefined dequeue methods in STL are push_front(), pop_front(), push_back(), front(), back(), front(), back(), size(), etc.

Forward List

A forward list STL in C++ is a container which implements the singly linked list. It was introduced in C++ 11 and provides constant average time insertion and deletion at the beginning of the list. However, it does not allow random access of elements.

 

Forward List STL In C++
#include <forward_list>

int main() {

  

  forward_list<int> numbers;

  numbers.push_front(1);

  numbers.push_front(2);

  numbers.push_front(3);

  

  int item = numbers.front();

  

  numbers.pop_front();

  cout << item << endl;

  numbers.push_front(4);

 for (auto & it : numbers) {

      cout << it<< ” “;

  }

  cout << endl;

  return 0;

}

 

Also, more STL containers are available, which are used by developers and programmers worldwide. C++ programming language, due to the STL support, is preferred even in big tech companies. There are many more features of C++, which you can check by clicking on the link.

FREQUENTLY ASKED QUESTIONS (FAQs)

 

Q1. What is the full form of STL In C++?

Ans: The C++ STL stands for Standard Template Library. It contains various containers such as vectors, stacks, maps, etc. It helps to implement various data structures effectively. Read the complete article to know more.

Q2. What are some of the major STL containers in C++?

Ans: Some of the significant containers in C++ are vectors, arrays, Stacks, queues, dequeues, forward lists, maps, etc. Complete STL containers are covered in the article above.

Q3. What are the three major libraries of C++?

Ans: The three important libraries in C++ are the General utility library, the diagnostic library, and the language support library.

 

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.