×

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

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Quick Sort in C++

Quick sort is an efficient, in-place, comparison-based sorting algorithm that uses a divide-and-conquer strategy to sort an array or list of elements. First a pivot element is selected from the...

4 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.