Professional Documents
Culture Documents
Standard Template Library
Standard Template Library
Standard Template Library
Why STL?
1. Reduce the amount of coding
2. Code reusability
3. Code Efficiency
4. Less Complexity
What is Template?
We create generic function(independent from data type) using templates
There are two types of templtes:
1. Function Template
2. Class Template
Example: Template function
Syntax:
template <typename T>
T functionname(T arg)
{
#include <iostream>
using namespace std;
return 0;
}
Class Template:
template <typename P, typename Q>
class c
{
P a,b:
Q c,d;
public:
P add()
{ return a+b;}
}
Int main()
{
c <int, float>obj1;
c <float> obj2;
int result=obj1.add(2,3);
float result1=obj2.add(2.3,4.5);
}
Categories of container:
1. Sequence container: vectors, list(linked list)
2. Container adapters: stack, queue and priority queue
3. Associative containers: bitsets, sets, multisets, maps, multimaps
Iterators:
1. It is a public member of container class
2. It is used to retrieve and insert data from the contain
3. For different containers there can be same iterator
4. There are generally 4 different type of iterators
a. Forward
b. Backward
c. Bidirectional
d. Random access
Algorithms:
1. Set of codes to manipulate data with in the container
2. We use generic(independent of data type) algorithms in STL
3. Algorithms operate over iterators rather than containers
4. There are 2 categories of algorithm
a. Mutating: these algorithms changes data within the container ex:
push_back()(it is used to insert data in vector)
b. Non_Mutating: ythese algorithm can not change the data in the
container ex: size()(it generally tells the number of elements in the
vector)
Vectors:
Why vectors?
-no limits
-dynamic
-continuous memory locations
-accesing element in const time
- if manipulating vector(i.e searching, inserting) it will take linear
time(depends on number of elements in the vector)
-vector is generic class template
-all the arithmetic and comparison operators works on vectors
vector<int> v,vector<int> b
if(v<b); it compares 2 vectors lexicographically(dictionary order)
Syntax:
template <typename T>
class vector{
vector()
{
}
}
#include<vector>
#include<bits/stdc++.h> it include the complete STL library
Ex:
vector <int> v;
v is vector object of datatype int, if no arguments is passed with the
vector object then it contains 0 element ond 0 memory.
vector <int> v(10) 10 integer blocks assigned to vector object v.
vector <int> v2(v) it copies all the element of vector v in vector v2.
vector <int> v3(10,2) 10 integer memory blocks assigned with all bocks
contains value 2.
Ex:
vector <int> v{1,2,3,4,5}
for(int i=0;i<5;i++)
cout<<v[i] <<” “ ; 12345
int x=v.size();
cout<<x<<end; 5
Vector Operators:
==operator:
Vector <int> v{1,2,3};
Vector<int> c{1,2,3,1};
If(v=c)---false( it checks for the same size of both the vectors, then it
checks each element index wise if all the elements in both vectors are
same then it returns true otherwise false)
>,<,>=,<=operator:
These operators are checked lexiciogrphically
cout<<v.front()<<end; 1
cout<<v.begin()<<end; address of location carrying data 1
Practicing vectors
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> a;
a[0]=2;
cout<<a[0]<<endl;
return 0;
}
Segmentation Error: as vector size is 0 we can not acces its 0th element i.e.
a[0]
int main()
{
vector<int> a;
a.push_back(2);
cout<<a[0]<<endl;
return 0;
}
Output : 2
int main()
{
vector<int> a;
a.push_back(2);
cout<<b[0]<<endl;
return 0;
}
Output: 2
int main()
{
vector<int> a;
a.push_back(2);
vector<int> b(a);
b[0]=6;
vector<char> c(a);
cout<<c[0]<<endl;
return 0;
}
Output: error
int main()
{
vector<char> c(5,66);
cout<<c[0]<<c[1]<<endl;
return 0;
}
Output: BB
int main()
{
//int n;
//char x;
vector<int> a;
a.push_back(2);
a.push_back(4);
a.push_back(5);
a.push_back(7);
a.push_back(3);
vector<char> c(5,66);
vector<int> d(a.begin(),a.end());
cout<<d[1]<<c[1]<<endl;
return 0;
}
Output: 3B
int main()
{
//int n;
//char x;
vector<int> a;
a.push_back(2);
a.push_back(3);
vector<int> b(a);
b[0]=6;
vector<char> c(5,66);
vector<int> d(a.begin(),a.end());
cout<<a.at(2)<<endl;
return 0;
}
vector<int> b(a);
b[0]=6;
vector<char> c(5,66);
vector<int> d(a.begin(),a.end());
cout<<(a==b)<<endl;
return 0;
}
Output: 0
int main()
{
//int n;
//char x;
vector<int> a;
a.push_back(2);
a.push_back(3);
vector<int> b(a);
b[0]=6;
vector<char> c(5,66);
vector<int> d(a.begin(),a.end());
cout<<(a<b)<<endl;
return 0;
}
Output: 1
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
cout<<a.at(6)<<endl;
return 0;
}
Output: 7
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
cout<<a.back()<<endl;
return 0;
}
Output: 10
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
cout<<a.end()<<endl;
return 0;
}
Output: error
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
vector<int> b(a);
a.clear();
cout<<a[3]<<endl;
cout<<a.at(3)<<endl;
return 0;
}
Output: 4
error
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
vector<int> b(a);
a.clear(); // doent clear the memory but remove the elements only
cout<<a.size()<<endl;
cout<<a.capacity()<<endl;
return 0;
}
Output: 0
10
Note: size<= capacity()
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
vector<int> b(a);
a.clear();
cout<<a.empty()<<endl;
return 0;
}
Output: 1
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
vector<int> b(a);
a.clear();
a=b;
cout<<a.size()<<endl;
cout<<a.capacity()<<endl;
return 0;
}
Output: 10
10
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
vector<int> b(a);
a.clear();
a=b;
for(int i=0;i<a.size();i++)
cout<<a.at(i)<<” “;
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
Iterators:
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
vector<int> b(a);
a.clear();
a=b;
// for(int i=0;i<a.size();i++)
// cout<<a.at(i)<<endl;
vector<int>::iterator it;
for(it=a.begin();it!=a.end();it++)
cout<<*it<<"";
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
int main()
{
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i+1;
vector<int> b(a);
a.clear();
a=b;
// for(int i=0;i<a.size();i++)
// cout<<a.at(i)<<endl;
vector<int>::iterator it;
for(it=a.end();it!=a.begin();it--)
cout<<*it<<"";
return 0;
}
Output: garbage 10 9 8 7 6 5 4 3 2
Vector<int> v{1,2,3,4,5};
Ex:
for(it=v.begin();it!=v.end();it++)
cout<< *it <<” “;
container iterator
vector random access iterator
List(linked list) bidirectional iterato
deque random access
Maps,sets,multisets bidirectional
Queue,stack no iterator