top of page
Writer's picturepreetikatiyar5

A Brief Introduction about Standard Template Library (STL)

Updated: Apr 10, 2023


STL stands for the Standard Template Library. It is a collection of C++ template classes that provide common data structures and functions with which to manipulate them. These data structures include vectors, lists, queues, and stacks, among others. The functions provided by the STL include operations such as sorting, searching, and manipulating the data stored in these data structures. The STL is designed to be efficient, flexible, and easy to use, making it a valuable tool for C++ programmers.


STL has 4 components –

  • Algorithms

  • Containers

  • Functions

  • Iterators


Algorithms

An algorithm is a set of instructions or rules that are followed in order to perform a specific task or solve a problem. It is a step-by-step process for solving a problem or completing a task. Algorithms are used in many different fields, including computer science, mathematics, and engineering. They are essential for solving complex problems and making efficient use of resources.

  • Search algorithms, which are used to search for specific items or values in a dataset. These algorithms can be applied to various problems, such as finding a particular element in an array or searching for a specific route on a map.

    1. Linear Search

    2. Binary search

    3. Hashing

There are many more such searching algorithms in the world of programming.

  • Sorting algorithms, which are used to organize a dataset in a specific order. These algorithms can be applied to various problems, such as sorting a list of numbers in ascending or descending order.

    1. Bubble Sort

    2. Selection Sort

    3. Merge Sort

    4. Quick Sort

There are various sorting algorithms, which differs in the Time & Space Complexity of the algorithms otherwise work of all such algorithms performs the same task.

This is not the end of algorithms, there are end number of algorithms with their defined functionalities and depending upon their purpose of use.





Containers

In the C++ programming language, the Standard Template Library (STL) includes a type of container called a vector. Vectors are sequences of elements that can be accessed using an index, and they provide efficient insertion, deletion, and resizing operations.

Some common types of STL containers include:

  • Sequence Containers

    1. Vector

    2. List

    3. Deque

    4. Array

    5. Forward_list

  • Container Adaptors

    1. Queue

    2. Priority_queue

    3. Stack

  • Associative Containers

    1. Set

    2. Multiset

    3. Map

    4. Multimap

  • Unordered Associated Containers

    1. Unordered_set

    2. Unordered_multiset

    3. Unordered_map

    4. Unordered_multimap






Functions

A function is a block of code that performs a specific task and is given a name. Functions are defined using the keyword ‘void’ or the data type of the value that the function will return, followed by the function's name and a set of parentheses ‘()’. The code that makes up the function is placed within a pair of braces ‘{}’. Here is an example of a simple C++ function that calculates the area of a rectangle :

int rectangle_area(int length, int width) {

return length * width;

}


In this example, the function is named ‘rectangle_area’, and it takes two arguments, ‘length’ and ‘width’, which are used to calculate the area of a rectangle. The function returns an ‘int’ value, which is the calculated area of the rectangle. To call the function, we would use its name followed by the values for the ‘length’ and ‘width’ arguments in the parentheses :

int main() {

// Calculate the area of a rectangle with length 5 and width 10

int area = rectangle_area(5, 10);

// Print the area to the console

std::cout << "The area of the rectangle is: " << area << std::endl;

return 0;

}


In this example, the ‘rectangle_area’ function is called with the values ‘5’ and ‘10’ for the ‘length’ and ‘width’ arguments, respectively. The function calculates the area of the rectangle and returns the result, which is then stored in the ‘area’ variable. Finally, the area value is printed to the console using the ‘cout’ statement.

The output of the above demonstration -

The area of the rectangle is: 50


Types of functions –

  • User Defined Function –

User-defined functions are user/customer-defined blocks of code specifically tailored to reduce complexity in large programs. They are also commonly known as "tailor-made functions" that are designed only to satisfy the situation the user is facing while reducing the complexity of the overall program.




  • Library Function –

Library functions are also called "built-in functions". These functions are part of a predefined compiler package and consist of special functions with special and different semantics. Built-in functions have the advantage that you can use them directly without defining them, whereas user-defined functions require you to declare and define the function before you can use it.

for example:

sqrt(), setw(), strcat(), etc.


There are two ways to pass parameters –

  • Pass by Value – In this parameter passing technique, the value of the actual parameter is copied into the function's formal parameter, and the two kinds of parameters are stored in different places. Therefore, changes made inside the function are not reflected in the caller's actual parameters.


  • Pass by Reference – Both the actual and formal parameters refer to the same location, so changes made within the function are actually reflected in the caller's actual parameter.



Iterators

In C++, an iterator is an object that allows you to traverse through the elements of a container, such as a vector or a list. It provides a way to access the elements of the container sequentially, without the need to know the underlying structure of the container. For example, you can use an iterator to iterate over the elements of a vector and print each element to the screen.

Here is an example of using an iterator to traverse through the elements of a vector in C++ -

#include <iostream>

#include <vector>

using namespace std;

int main()

{

// Create a vector and add some elements to it.

vector<int> myVector;

myVector.push_back(1);

myVector.push_back(2);

myVector.push_back(3);

// Get an iterator for the vector.

vector<int>::iterator it;

// Use the iterator to traverse through the vector and print each element.

cout << "Vector elements: ";

for (it = myVector.begin(); it != myVector.end(); it++)

{

cout << *it << " ";

}

cout << endl;

return 0;

}


Output –

Vector elements: 1 2 3


In this example, we create a vector and add some elements to it. Then we create an iterator for the vector using the ‘vector<int>::iterator’ type, which is a type that is defined by the vector class. We use this iterator to traverse through the elements of the vector, and print each element to the screen.

The ‘begin()’ and ‘end()’ methods of the vector are used to get the iterator that points to the first element of the vector and the iterator that points to the element that comes after the last element of the vector, respectively. The ‘*’ operator is used to dereference the iterator and get the value of the element that it points to.

Learn more about STL – Click here

List of C++ Container Function –




Also, read – Best C++ IDEs in 2023

30 views

留言


Subscribe to Our Newsletter

Thanks for submitting!

bottom of page