What is Standard Template Library (STL) in C++

We hope you have already understood Templates in C++ before knowing about STL(Standard Template Library) in C++. If you don't, then please understand the concept of C++ templates, it will help you understand STL very easily.

What is STL in C++

1. STL is a Standard Template Library in C++.

2. It is a powerful set of C++ template classes.

3. It helps implement popular and commonly used algorithms and data structures.

4. STL contains different pre-defined functions that help you perform various complicated tasks in a very easy manner.

5. At the core of the C++ Standard Template Library are three well-structured components (containers, algorithms, and iterators).

Standard-Template-Library-STL-in-c++

Components of STL

There are 3 main components of STL in C++. They are:

1. Containers
2. Algorithms
3. Iterators


Containers

1. Containers are used to manage groups of objects of a certain kind.

2. Container classes store data and objects.

3. The container library in STL provides containers that are used to create data structures like arrays, linked lists, trees, maps, etc.

4. These containers are generic and can hold elements of any data type.

Example: Vector can be used to create dynamic arrays of char, integer, float, and other types.

STL-Containers-in-C++

Some Common Containers

1. Vector: replicates arrays.
2. Queue: replicates queues.
3. Stack: replicates stack.
4. Priority_queue: replicates heaps.
5. List: replicates linked list.
6. Set: replicates trees.
7. Map: associative arrays.


Classification of Containers

1. Sequence Containers
2. Associative Containers
3. Unordered Associative Containers
4. Containers Adapters


Sequence Container

1. Sequence containers are used for data structures that keep objects of the same type in a linear sequence.

2. It implements data structures that are accessed sequentially.

Example: array, vector, forward_list, list, deque.


Associative Container

1. Associative containers implement data structures that are quickly searched.

2. It can be classified in two ways namely containers that require a unique key and containers that allow multiple entries using a name key.

3. It has the complexity of O(log n).

Example: set, map, multiset, multimap.


Unordered Associative Containers

1. It provides unsorted data structures that are accessed using a hash. 

2. The access times are O(n) in worst-case and can be reduced to O(1)  but it is much faster than that of linear time for most operations.

3. For each and every STL Unordered Associative Container, a hashed key is used for accessing the data.

4. Like an Associative Container, it can also be classified into two types (one which requires unique keys and others which do not require unique keys).

5. You can override the hashing function, key comparison function, and allocator if you want but remember you must override it during its declaration.

Example: unordered set, unordered_map, unordered_multiset, unordered_multimap.


Container Adapters

1. Container adapters are not full container classes on their own, but wrappers around another container like vector, list, or deque.

2. It simply provides a different interface for sequential containers.

Example: stack, queue, priority_queue.


Algorithms

1. Algorithms act on containers.

2. Algorithms allow you to perform initialization, sorting, searching, and transforming containers' contents.

3. Algorithms library contains several built-in functions that perform complex algorithms on the data structures.

4. It provides abstraction which means you do necessarily need not to know how the algorithm works.

Example: you can reverse a range with the reverse() function. You can sort a range with the sort() function and you can search in a range with binary_search().


Iterators

1. Iterators are used to step through the elements of collections of objects. These collections may be containers or subsets.

2. Iterators in STL are used to point to the containers.

3. Iterators actually act as a bridge between containers and algorithms.

Example: sort() function has two parameters  ( one is starting iterators and another is ending iterators). sort() function compares the elements pointed by each of the two iterators and arranges them in sorted order.

Thus, it does not matter what is the type of container and the same sort() function can be used on different types of containers.


What is STL in C++ Example:
#include<iostream>
#include<vector>
using namespace std;
 
int main() 
{
 //Create a vector to store int
 vector <int> vec; 
 int i;

 // display the original size of vec
 cout << "vector size = " << vec.size() << endl;

 //Push 5 values into the vector
   for(i = 0; i < 5; i++) {
      vec.push_back(i);
   }

   // display extended size of vec
   cout << "extended vector size = " << vec.size() << endl;

   // access 5 values from the vector
   for(i = 0; i < 5; i++) {
      cout << "value of vec [" << i << "] = " << vec[i] << endl;
   }

   //Use an iterator to access the values
   vector <int> :: iterator v = vec.begin();
   while( v != vec.end()) {
      cout << "value of v = " << *v << endl;
      v++;
   }

   return 0;
}

Output:
vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4

push_back() function inserts value at the end of the vector.
size() function displays the size of the vector.
begin() function returns an iterator to the start of the vector.
end() function returns an iterator to the end of the vector.




Labels

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.