Standard Template Library

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

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;

template <typename T>


T add(T num1, T num2) {
return (num1 + num2);
}
int main() {
int result1;
int result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;

// calling with double parameters


result2 = add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;

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);
}

STL( Standard Template Libraries)


Three major components:
1. Container-used to contain data
2. Iterators—pointers to access data from the containers(pointer and pointer
arithmetic)
3. Algorithm--- lines of codes to process data in the container

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

Generic Algorithms used with vectors


1. at() : vector<int> v{1,2,3,4,5};
v.at(4) 5 advantage of using at() v.at(5) –error is it doesn’t let us use out
of the boudaries values
v[4] 5 v[5]---garbage value

2. front() and back() /begin(),end():


a. front(): it retuns the reference to the first element of the vector
b. back(): returns the reference of the last element of the vector
c. begin(): it returns the iterator pointing towards the first element of
the vector
d. end(): it returns the iterator pointing next to the last element of the
vector
ex: vector<int> v{1,2,3,4,5}

cout<<v.front()<<end; 1
cout<<v.begin()<<end; address of location carrying data 1

3. clear(), empty(),erase(iterator start, iterator end) v.clear() clears


value in the vector but didn’t erase the memory allocated
4. push_back(): enters the value at the end of the vector and pop_back():
deletes the value at the ened of the vector
int x=v.pop_back(); //error
v.push_back(3);
5. insert(iterator loc, const type val)) v.insert(v.begin(),32);
6. size() and resize() int x=v.size();
7. capacity() and reserve() v.capacity()
8. assign() v.assign(3) vector<int> v(4,3);
9. swap() v.swap(c )

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);

vector<int> b(a); / special type of constructor is called copy constructor

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;
}

Output: throws exception


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: 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};

Vector<int>:: iterator it; it is the iterator of vector<int>;

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

You might also like