×

C++ STL Components

C++ STL Components

In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to learn many things in simplest way possible.

Let’s first understand the full form of the STL in C++. So STL stands for Standard template library.

 STL definition

Standard template library (STL) in C++ is the library which provides us the set of C++ template classes which further provides us the generic classes and functions that we are using in implementation of data structures and algorithms.

Components of STL

So basically STL contains four components and those are given below:

  1. Containers.
  2. Algorithms
  3. Iterators
  4. Function object.

These four components are further described properly.

  1. Containers:-

Containers are a one way of organizing data in memory. In other words, we can also say that a container holds the data which is of same type.

Example – arrays, tree, list etc. we can implement these data structures using containers.

Containers are further divided into three classifications and those are:-

  • Sequence containers
  • Associative containers
  • Derived containers
  • Un-ordered Associative containers.

Sequence container:-

The sequence container is used to implement Data structure which need to be accessed in a sequential manner. Examples would be:- Vector, List, deque.

Associative container:-

Associative container is used to implement the stored data structure. Example would be- Multiset, Set, Map, multimap.

Un-ordered Associative containers:-

Un-ordered Associative containers are used to implement un-stored data structure. Example would be- Unordered -Multiset

Unordered -Set

Unordered -Map

Unordered –multimap

Derived containers:-

Derived containers are used to provide different interface to the sequence containers in C++. Example would be- Queue

Stack

Priority_queue

Here are following containers with description, header file and iterator.

ContainersDescriptionHeader fileiterator
VectorVector is also known as class in C++ STL which allows insertion and deletion at the back.<vector>Random access  
DequeDeque us basically a double ended queue which allows us the insertion and deletion at both side.<deque>Random access
ListWe can also refer to list as a sequence container which allows us the insertion and deletion from anywhere.<list>Bidirectional  
SetWe can also refer to set as a associate container for storing the unique sets.<set>Bidirectional.
MultisetMultiset is also a associate container for storing the non-unique sets.<sets>Bidirectional  
StackStack follows last in and first out (LIFO)<stack>No iterator
QueueQueue follows first in and first out (FIFO)<queue>No iterator
Priority_queueIn Priority_queue, first element is the highest priority element always.<queue>No iterator
MapMap is also an associate container to store unique key- value pair.<map>Bidirectional
MultimapIt is also associate container for storing key value pair, and each key can associate with more than one value<map>Bidirectioanl

Iterator:-

When we need to access the individual element in the container then we use iterator. Because iterator is referred as the pointer-like entities. When iterator is moving sequentially from one element to other element then this process is also known as iterating through containers.

Only two functions contain by iterator. Those are begin and end.

Begin():a member function begin() return the iterator to the first element of the vector.

End(): a member function end() returns the iterator to past the last element of that container.

Algorithms Definition

Standard template library (STL) in C++ provides us many algorithms that can be used with any of the container and the type of container will not affect the operation.

Algorithms are the library which contains built in function that can perform complex algorithms on the data-structure.

Example of some of the algorithms which are present in the algorithm library:

  • Sort()  = Which input or range we are giving, this algorithm helps in sorting. In either ascending order or descending order.
  • Reverse() = This function reverses the range.

In algorithm, there are approximately 60 algorithms (function), and these functions help in performing complex operations.

We are also allowed to work with two different algorithms at a same time.

These algorithms help in saving efforts and time. To trigger these algorithms, we need to include <algorithm> header file in the program first.

Types of algorithms in Standard template library (STL) in C++ areas follow:

  1. Sorting Algorithm.
  2. Mutating Algorithm.
  3. Non-Mutating Algorithm.
  4. Set Algorithm.
  5. Relational Algorithm.    

These algorithms are further described briefly.

  1. Sorting Algorithm: In Standard template library (STL), sorting algorithm helps in modifying the algorithms which is used to sort the elements in the container.
  • Mutating Algorithm: In Standard template library (STL), mutating algorithm is used to change the order of any element in which it is representing by altering the value of the container.
  •  Non-Mutating Algorithm: In Standard template library (STL), Non- Mutating Algorithm means that opposite of mutating algorithm which means non mutating algorithm does not change the order of any of the element which is present in the container nor it alter any value of the container.
  • Set Algorithm: In Standard template library (STL), Set Algorithm helps to perform some functions on the container which improves the efficiency of the program that is why this algorithm is also well-known as sorted range algorithm.
  • Relational Algorithm: In Standard template library (STL), Relational Algorithm are basically those algorithms which perform all the mathematical operation on the elements which is present in the container so in other words we can also say that Relational Algorithm is used to perform operation on numerical data.

Iterators Definition

Basically, iterators are used to point at the memory address of the Standard template library (STL) containers, and they are primarily used in sequence of int, char etc.

In other words, we can also refer to iterators of Standard template library (STL) as whenever we need to access the individual element in the container, we use iterator. Because it referred as the pointer-like entities.

Why iterators?

  1. We use iterators because it helps in reducing the complexity of the program.
  2. It also helps in executing the program in less time than iterators.

Iterator Syntax

Container_type<parameter_list>::

Iterator   iterator_name;

Different operations which are performed on iterators in Standard template library (STL) are as follows:

  1. Advance()
  2. Distance()
  3. Next()
  4. Previous()
  5. Begin()
  6. End()

Let’s discuss about the details of these operations.

  1. Advance():- The advance() operation means that it will increment the iterator I by the value of the distance and if the value of the distance is negative then instead of incrementing it , it will decrement.

Syntax would be: advance(iterator I, int distance)

  • Distance():- distance operation means that it will help in returning the number of elements or the distance between first and last iterator and these both are the same.

Syntax would be: distance(iterator first, iterator last)

  • Next():- next() means it will return nth iterator to i, which in simple words means that iterator pointing to nth element from the element pointed by the i.

Syntax would be:- next(iterator I, int n)

  • Previous():- prev() means that it will return nth predessor to i, which in simple words means that iteator pointing to nth predessor element from the element pointed by i.

Syntax would be:- prev(iterator I, int n)

This is same as the next() but the only difference here is that int n here is pointing the nth predessor.

  • Begin():- begin() means it brings the iterator to the start of the given container.

Syntax would be:-  begin()

  • End() :- end() means it returns the iterator to the end of the given container.

Syntax would be:- end()

These are the different operations which are performed on the iterators in Standard template library (STL)


Related Topics

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

C++ Output Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

4 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

LCM Program in C++

What is LCM? LCM is an acronym for "least common multiple". It is used to discover the lowest positive integer divisible by all integers (whose LCM is calculated). For instance, the...

4 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.