Usage Of STL In CPP Programming

Basic Overview

Basically, the STL is an acronym of Standard Template Library that is considered as the bundle of template classes and functions. Moreover, we can state the STL as just an internal implementing library provided in C++ usually for the generic programming approach.

Significance of STL

  1. Firstly, taking the STL into practice by the users somewhere enhance the coding capability. As it is highly focused on the generic approach so it promotes code re-usability.
  2. STL are even used in the competitive programming that also saves the time and effort of the user at a higher level.
  3. These libraries are even extensible up to some level, so the user can change the functionality according to their respective needs that increase efficiency of the functions.
  4. The predefined functionalities in these libraries in the forms of the algorithms and data structures, served to the user the required outcome will be more productive through this.

Components in STL

The STL is mainly the formation of these four major components that usually constitute the standard library.

  1. Containers      
  2. Algorithm  
  3. Iterators
  4. Functors

Let us understand objective and purpose of these components in some brief.

(i) Containers

These are usually the data structures that are pre-implemented in STL. In simple terms, the container is defined as a type of holding element that stores or holds the set of other elements/objects in it and provides them some space to exist, and enables member function to access these elements.

These containers are further bifurcated into different segments  -

  • Sequence  -  array, vector, deque, list.. - These are sequentially implemented whether static or dynamic
  • Ordered Associative - set, map, multiset, multimap.. - Here the values are stored in some set of order, ascending or descending that can quickly be accessed
  • Containers Adaptor - stack, queue, priority queue.. - Simply an extension to the containers 
  • Unordered Associative - unorderSet, unorderMap..  - Unsorted data structures, based on the hashing algorithms, derived by hash.

There are also n numbers of defined functions available for the specific containers in their own library. Let us know them in on go manner.

Array, we know its a static linear data structure that stores the elements in a contiguous memory allocation.

First we will include library,  and can easily perform these basic operations over the data elements.

#include<array>
  1. at() - used to print the value at specific Index.
  2. size() -  return the size of the array.
  3. front() - print the element at beginning.
  4. back() - print the element at last.
  5. empty() - check whether array is empty or not.

Now, consider the code to get logical understanding of the operations on the array as well

#include<iostream>
#include<array>     // this library holds the different functions  for array

using namespace std;
int main()
{
array<int, 5> odd = {11,33,55,77,99};   //   <datatype, size> array_name = {};

 for(int i=0 ; i<5 ; i++)
 {
   cout<<odd[i]<<endl;   // will print the elements stored in array 'odd'
 }
 cout<<"The size of the array is\n"<<odd.size()<<endl;  
 cout<<"Is it Null or Not\n"<<odd.empty()<<endl;
 cout<<"Item at the First Position\n"<<odd.front()<<endl;
 cout<<"Item at the First Position\n"<<odd.back()<<endl;
 return 0;
}

The output for the same code is as follow

Vectors, are somewhere considered as dynamically sequenced based container, that allocates memory at run time only.

Again, we have to include its library first, and easily perform the operations.

#include<vector>
  1. size() - will returns the size of vector.
  2. capacity() - will returns the current memory assigned.
  3. push_back() - insert the element into defined vector.
  4. pop_back() - remove the element from defined vector. 

Following code will elaborate the function used over the vector

#include<iostream>
#include<vector>      //this library includes the different operation of vector
using namespace std;
int main()
{
   vector<int> v1;     
   v1.push_back(5);   // will insert the element '5' in the vector
   cout<<"The capacity is  "<<v1.capacity()<<endl;

   v1.push_back(10);
   cout<<"Now the updated capacity is  "<<v1.capacity()<<endl;

   v1.push_back(15);
   cout<<"Current capacity is  "<<v1.capacity()<<endl;
   cout<<"Size of Vector is "<<v1.size()<<endl;
   
   v1.pop_back();    // will remove the element
   cout<<"After removing, size of Vector is "<<v1.size()<<endl;
   cout<<"The element at index 1st is  "<<v1.at(1)<<endl;
   return 0;
}

The output for the same code is as follows

As shown above, the capacity of the vector doubles as soon as the elements are inserted through push_back. Although it will get back to decreasing as the items are removed using the pop_back functions. Likewise, many different functions are also defined for the different containers as well. Now coming to the Algorithms

(ii) Algorithms

Mainly the algorithms are considered as a set of instructions that are used to solve some task. In STL, the n numbers of the algorithms are already defined that are required for different working operations.

<algorithm> library holds a different type of algorithms in it that are supposed to do various tasks from even low to high.

search(), sort(), max(), min(), reverse(), find(), accumulate() are different built-in algorithms present in the libraries. The usage of these algorithms in programming will leads to the specialization in different ways or in different sectors.

Understand the Binary Search using algorithm function through the following program

#include<iostream>
#include<algorithm>   // this library hold different kinds of algorithms
#include<vector>
using namespace std;
int main()
{
   vector<int> v1;
   v1.push_back(5);
   v1.push_back(8);
   v1.push_back(13);
   v1.push_back(25);
   v1.push_back(53);
   v1.push_back(75);
   
   cout<<"The entered element in vectors are"<<endl;
   for(int i:v1)
   {
       cout<<i<<" ";
   }
   cout<<endl;
   cout<<"Searching the element 25  : "<<binary_search(v1.begin(), v1.end(),25)<<endl;
   cout<<"Searching the element 55  : "<<binary_search(v1.begin(), v1.end(),55)<<endl;
   cout<<"Searching the element 13  : "<<binary_search(v1.begin(), v1.end(),13)<<endl;

   // this will pass the boolean values as a result to signify availabilty of elements
   return 0;
}

The output for the same code is as follows

As it returns the boolean value, where the 1 represents True and the 0 signifies the False condition.

Finding Maximum and Minimum using the functions defined

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
  int a = 55;
  int b = 45;
  cout<<"The Max among the Two is "<<max(a,b); // here max is predefined algorithm 
  cout<<"The Min among the Two is "<<min(a,b); // here min is predefined algorithm 
  return 0;
}

The output for the same code as follows

The following program is for Sorting Vector using sort() algorithm function

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
   vector<int> v1;
   v1.push_back(55);
   v1.push_back(98);
   v1.push_back(13);
   v1.push_back(125);
   v1.push_back(53);
   v1.push_back(65);

   cout<<"The entered element in vectors are"<<endl;
   for(int i:v1)
   {
     cout<<i<<" ";
   }
   cout<<endl;
   cout<<"The vectors element in sorted manner "<<endl;
   sort(v1.begin(), v1.end());    // both begin() and end() iterators are used.
    for(int i:v1)
    {
      cout<<i<<" ";
    }
    cout<<endl;
    return 0;
}

  

The output for the same code is as follows

(iii) Iterators

The working of the iterator is just similar to the role of the pointer in ‘C’. An iterator is designed and is mainly required to point to the memory address of the containers in STL. These are used to traverse through the contents of the STL containers.

The different types of iterators are used accordingly to the different workings, some of the iterators are Input iterator, Output iterator, Forward, Bidirectional iterator. Mainy the common two main functions of these iterators are -

  • begin() - This mainly visualize the beginning of the elements in STL container, which is the position of first item.

  • end() - This depicts the end of the elements in STL container, which is the position just before the last item in container.

(iv) Functors

Coming to this, just consider the functors are basically the classes with can be used as a Function or we can state them as a type of the function wrapped in a class so that it can be accessed likewise as an object. The usage of these are merely low as compared to the other three.

Summary 

Lastly, we can conclude that the STL is a productivity increasing segment that is much more suitable for opting the generic approach in the programming and even very helpful for the workings in a competitive environment as its full-fledged bundle of pre-implemented and defined context.


Similar Articles