STANDARD TEMPLATE LIBRARY

Standard Template Library (STL):

a) STL provides the built-in implementation of commonly used data structures known as containers.
b) They are implemented as class templates.
c) A container manages the storage space for its elements and provides member functions for easy access for useful operations.

I) Sequence Containers:
Sequence Containers implements linear data structures in which the elements can be accessed sequentially.

a) Array -> Container that wraps over fixed size Static Array

#include <array>
#include <tuple>

array<int, 5> arrObj{ 1,2,3,4,5 };

cout << "array at(1) : " << arrObj.at(1)<<endl;
cout << "array get(4) : " << get<4>(arrObj)<<endl;

cout << "array - []" << endl;
for (int i = 0; i < 5; i++)
{
cout << "arrObj[" << i << "] = " << arrObj[i] << endl;
}

cout<<"array - iterator"<<endl;
for (array<int, 5>::iterator it = arrObj.begin(); it != arrObj.end(); it++)
{
cout << *it << ", ";
}

b) Vector -> Automatically resizable dynamic array

#include <vector>

vector<int> vectObj;

vectObj.push_back(1);
vectObj.push_back(2);
vectObj.push_back(3);
vectObj.push_back(4);
vectObj.push_back(5);

cout << "vector - []" << endl;
for (int j = 0; j < 5; j++)
{
cout << "vectObj[" << j << "] = " << vectObj[j] << endl;
}

cout << "vector - iterator" << endl;
for (vector<int>::iterator it1 = vectObj.begin(); it1 != vectObj.end(); it1++)
{
cout << *it1 << ", ";
}

c) List    -> Implementation of Doubly linked list data structure

#include <list>

list<int> listObj;

listObj.push_back(1);
listObj.push_back(2);
listObj.push_back(3);
listObj.push_back(4);
listObj.push_back(5);

cout << "list - iterator" << endl;
for (list<int>::iterator it2 = listObj.begin(); it2 != listObj.end(); it2++)
{
cout << *it2 << ", ";
}


II) Associative Containers:
a) Associative Containers store data in some sorted order.
b) It provides fast search, insert and delete.

a) Set   -> Collection of unique elements sorted on the basis of their values.

#include <set>

set<int> setObj;

setObj.insert(1);
setObj.insert(2);
setObj.insert(3);
setObj.insert(4);
setObj.insert(5);

cout << "set - iterator" << endl;
for (set<int>::iterator it2 = setObj.begin(); it2 != setObj.end(); it2++)
{
cout << *it2 << ", ";
}


b) Map   -> Collection of key-value pairs sorted on the basis of keys where no two pairs have same keys.

#include <map>

map<int, int> mapObj;

mapObj.insert(pair<int, int>(1, 10));
mapObj.insert(pair<int, int>(2, 20));
mapObj.insert(pair<int, int>(3, 30));
mapObj.insert(pair<int, int>(4, 40));
mapObj.insert(pair<int, int>(5, 50));

cout << "map - iterator" << endl;
for (map<int,int>::iterator it2 = mapObj.begin(); it2 != mapObj.end(); it2++)
{
cout << (*it2).first << ", "<< (*it2).second<<endl;
}


III) Container Adapters:
Container Adapters provides a different interface for other containers.

a) Stack -> Adapts a container to provide stack(LIFO) data structure.
   Only the top element can be accessed using top() method.

#include <stack>
   
stack<int> stackObj;

stackObj.push(1);
stackObj.push(2);
stackObj.push(3);
stackObj.push(4);
stackObj.push(5);

cout << "stack - iterator" << endl;
for (int j = 0; j < 5; j++)
{
cout << stackObj.top() << " ";
stackObj.pop();
}    

   
b) Queue   -> Adapts a container to provide queue(FIFO) data structure.

#include <queue>

queue<int> queueObj;

queueObj.push(1);
queueObj.push(2);
queueObj.push(3);
queueObj.push(4);
queueObj.push(5);

cout << "queue - iterator" << endl;
for (int j = 0; j < 5; j++)
{
cout << queueObj.front() << " ";
queueObj.pop();
}


Iterators in STL:
Iterators are used to loop through the contents of the STL containers.

<type>::iterator it;
begin() - end() 
rbegin() - rend() (reverse traversal)