C++ is a general-purpose programming language that can be used for many tasks. However, to make programming easier in C++, we use a library called the Standard Template Library (STL). The STL in C++ is a set of tools that includes different templates, which are ready-made structures like containers, iterators, algorithms, and function objects. In this tutorial, you will learn everything you need to know about the STL in C++ in a simple and easy-to-understand way.
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 STLs 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.
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.
1. 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; |
2. 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; } |
3. 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; } |
4. 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.
5. 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.
6. 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 |
7. 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.
8. 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.
Important Algorithms In C++
With STL in C++, we can use iterators to implement different types of algorithms. Algorithms are basically functions that work with containers and perform operations on their content. Some common examples of these algorithms include `sort()`, `swap()`, `min()`, and `max()`. These functions make it easier to manage and work with the data stored in containers.
Types of algorithms:
- Modifying algorithms
- Non-modifying algorithms
- Sorting algorithms
- Searching algorithms
- Numeric algorithms
Learn C++ With PW Skills
Enroll in our PW Skills C++ with DSA course to learn each and every concept related to C++ programming along with the topics of Data structure and algorithms in a beginner-friendly way.
This training course will offer you features like- Daily assignments, an updated industry-relevant curriculum, doubt-clearing sessions with experts, access to PW lab for coding, and much more.
Visit PWskills.com today to book your seat on this exciting journey.
Recommended Course
STL In C++ FAQs
What is the full form of STL In C++?
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.
What are some of the major STL containers in C++?
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.
What are the three major libraries of C++?
The three important libraries in C++ are the General utility library, the diagnostic library, and the language support library.