2. Standard Template Library
The C++ STL (Standard Template Library) is a
powerful set of C++ template classes to
provide general-purpose classes and
functions.
With templates that implement many popular
and commonly used algorithms and data
structures like lists, queues, and stacks.
3. C++ Standard Template Library are following three well-
structured components −
Sr. No Component & Description
1 Containers: (Organizing stored data in memory)
Containers are used to manage collections of objects of a certain kind.
There are several different types of containers like deque, list, vector,
map etc.
2 Algorithms: (Procedures applied to process their data)
Algorithms act on containers. They provide the means by which you will
perform initialization, sorting, searching, and transforming of the
contents of containers.
3 Iterators: (Point to the elements in a container)
Iterators are used to step through the elements of collections of
objects. These collections may be containers or subsets of containers.
5. Sequence container
-It is used to implement data structure which can
be accessed in a sequential manner.
Vector- It is same as dynamic arrays offer random
access back insertion.
Eg: int array[5]= {12,7,9,21,13};
Vector <int> v(array, array +5);
12,7,9,21,13 // V[3]=21
Functions of vector:- Push(), Pop().
V. Push_back(15); V.Pop_back(15);
12 7 9 21 13 15 12 7 9 21 13
6. //Program to demonstrate the vector container
#include<iostream>
#include<vector>
Using namespace std;
Int main()
{
Vector <int> V;
V. Push_back(1); //insert 1 at the back of V
V. Push_back(2); //insert 2 at the back of V
V. Push_back(4); //insert 4 at the back of V
For (Vector<int>:: iterator i=V, begin (1);
i!= V. End(); i++)
{
Cout <<*i <<””; // for printing the vector particular address location
}
}
O/P: 1 2 4
7. Vector Container Continue…
Program that demonstrates the vector
container which is similar to an array with an
exception that it automatically handles its
own storage requirements in case it grows.
8. //Program to demonstrates the vector container:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int vector<int> vec; // create a vector to store
int i;
cout << "vector size = " << vec.size() << endl; // display the original size of vec
for(i = 0; i < 5; i++) // push 5 values into the vector
{vec.push_back(i); }
cout << "extended vector size = " << vec.size() ; // display extended size of vec
for(i = 0; i < 5; i++) { // access 5 values from the vector
cout << "value of vec [" << i << "] = " << vec[i] << endl; }
vector<int>::iterator v = vec.begin(); // use iterator to access the values
while( v != vec.end()) {
cout << "value of v = " << *v << endl;
v++; }
return 0; }
9. Associative and unordered associative containers
• Associative containers:
-used to implement stored data structure
Eg. Map, multimap, set, multiset
Eg. (1) map: maps are used to replicate associative arrays. It
contains stored key values pair, in which each key is unique,
and cannot be changed , it can insert and delete but cannot
alter.
O[n] -time complexity
Syntax: Map <key-type, value- type> map-name;
keys values
125 xyz
10. //Creating a map in different ways
#include<iostream>
#include<map>
Using namespace std;
int main ()
{
Map <int, int> m{[1,2],[2,3],[3,4]};
// creates a map in with keys 1,2,3 and their corresponding values 2,3,4
Map < string , int> map1;
// creates a map with keys of type char & Value of type integer
Map1 [“abc”]=100; // insert key=”abc” with value =100
Map1 [“b”]=200; // insert key=”b” with value=200
Map1 [“c”]=300; // insert key =”c” with value =300
Map1 [“def”]=400; // insert key=”def” with value=400
Map <char, int> map2 (map1. Begin(),Map1. End()); //creates a map map2
which have entries copied from map1. Begin() to map1. End()
}
11. Container adapter:
-It is used to provide different interface to the
sequence container
Eg. Queue , priority queue, stack
Queue: queue container is used to replicate queue in
c++, insertion always back, deletion always front of
queue
Syntax: queue <obj-type> queue-name;
Function: Push (), Pop(), size(), swap()
Eg. Back of queue front of queue2
12. //Program to demonstrate the Queue container
#include<iostream>
#include<queue>
Using namespace std;
int main()
{
queue <int> q; // creates an empty queue of int q
q. push(2); // Pushes 2 in the queue, now front=back=2
q. push(3); //Pushes 3 in the queue, now front=2, back=3
q.push(5);
}
O/P: 5 3 2
13. Priority queue
-It is just like a normal queue except the element
removed from the queue is always the
greatest among all the element in queue.
• Syntax: priority_queue <int> pq;
• Function: push(), Pop(), size(), swap()
14. //Program to demonstrate the Priority queue container
#include< iostream>
#include<queue>
Using namespace std;
int main()
{
priority_queue <int> pq;
Pq. Push (30); //insert 30 to pq now top=30
Pq. Push (40); // insert 40 to pq now top=40
Pq. Push (90); // insert 90 to pq now top =90
Pq. Push (60); // insert 60 to pq top stills 90
Pq. Pop () ; // removes 90
}
15. Stack:
Stack:
- insertion and deletion always performed at
top of the stack.
Syntax: stack<obj-type> stack_name;
Function: push (), pop(), top(), size(), swap()
16. //Program to demonstrate the Stack
#include<iostream>
#include <stack>
Using namespace std;
int main()
{
Stack <int> s; //create empty stack
S. push (2); //pushing element into stack
S. push(3);
S. push(4);
Cout << s.top(); //print 4
Cout << s. size(); //print 3
}