stl algorithms

39
STL Algorithms

Upload: fawzmasood

Post on 12-May-2015

2.263 views

Category:

Education


1 download

DESCRIPTION

My 2nd semester OOP's and Data Structure presentation. STL Algorithms. Use MS powerpoint to open it.

TRANSCRIPT

Page 1: STL ALGORITHMS

STL Algorithms

Page 2: STL ALGORITHMS

Presented by:

• Fawz Masood E08-041• Faizan Sohail E08-035• Nauman Malik E08-075• Mohammad Bilal Firoz E08-058• Arsalan Abbasi (Group Leader)E08-021 Names are displayed in chronological order.

Page 3: STL ALGORITHMS

What is STL?>> STL STL stands for standard template library and is basically a library

of many useful containers or algorithms.>> In layman terms , it basically is a class template with functions

already created making life easier as they need to be merely called to be used. (Our definition)(Our definition)

>> The STL achieves its results through the use of templates. This approach is very powerful, delivering compile-time polymorphism that is often more efficient than traditional run-time polymorphism. Modern C++ compilers are tuned to minimize any abstraction penalty arising from heavy use of the STL.

>> The Standard Template Library was created as the first library of generic algorithms and data structures, with four ideas in mind: generic programming, abstractness without loss of efficiency, the Von Neumann computation model, and value semantics.

Page 4: STL ALGORITHMS

Uses of STL – Why should it be used?

> > The use of STL carries certain advantages , with obvious ones bieng the decreased usage of time.

(1) Reduce development time.– Data-structures already written and debugged.

(2) Code readability -Fit more meaningful stuff on one page.

(3)Robustness(4)STL data structures grow automatically.

(5)Portable code.(6)Maintainable code

Page 5: STL ALGORITHMS

The three parts of STL

>> Containers A container is a holder object that stores a collection other

objects (its elements). They are implemented as class templates, which allows a great flexibility in the types supported as elements.

>> Algorithms The header <algorithm> defines a collection of functions The header <algorithm> defines a collection of functions

especially designed to be used on ranges of elements.especially designed to be used on ranges of elements.

> > Iterators Iterators They are a generalization of pointers: they are objects that point They are a generalization of pointers: they are objects that point

to other objects. As the name suggests, iterators are often used to to other objects. As the name suggests, iterators are often used to iterate over a range of objects: if an iterator points to one iterate over a range of objects: if an iterator points to one element in a range, then it is possible to increment it so that it element in a range, then it is possible to increment it so that it points to the next element.points to the next element.

Page 6: STL ALGORITHMS

Introduction to STL Algorithms

>> An algorithm is a function that does something to the items in a container (or containers).

As we noted, algorithms in the STL are not member functions or even friends of container classes, as they are in earlier container libraries, but are standalone template functions. You can use them with built-in C++ arrays, or with container classes you create yourself (provided) the class includes certain basic functions

We will now present these functions to you in 5 sets of 5 functions each.The presentations will include :(i) What the function does. (ii) The parameters of the function(iii) An example

Page 7: STL ALGORITHMS

The sort() function(i) Sorts the elements in the range [first, last] into ascending order

(ii) Function parameters : Iterator first , Iterator last An example : #include<iostream>

#include<algorithm>void main(){int arr[10]={2,4,1,5,6,7,-11,23,9,-1999};

for(int a=0;a<10;a++){

cout<<arr[a]<<"\t";}cout<<endl;sort(arr,arr+10);for(a=0;a<10;a++){

cout<<arr[a]<<"\t";;}

}

Page 8: STL ALGORITHMS

Extended Example#include<algorithm>#include<iostream.h>#include<list.h>using namespace std;

void main(){

list <int> dc,dc2;

dc.push_back(9);dc.push_back(10);dc.push_back(-11);

dc2=dc;

while(! dc.empty() ){

cout<<dc.front();dc.pop_front();cout<<endl;

}

dc2.sort();cout<<endl;

while(! dc2.empty() ){

cout<<dc2.front();dc2.pop_front();cout<<endl;

}

}

Page 9: STL ALGORITHMS

The rotate() function(i) Rotates the order of the elements in the range [first,last), in such a way

that the element pointed by middle becomes the new first element.(ii) Function Parameters : Iterator first, Iterator middle ,Iterator last.

An example : #include<iostream>

#include<algorithm>

int arr[10]={1,2,3,4,5,6,7,8,9,10};rotate(arr,arr+4,arr+10);

for(int a=0;a<10;a++){

cout<<arr[a]<<"\t";}

cout<<endl;

[ Output will be “ 5 6 7 8 9 10 1 2 3 4 5”]

Page 10: STL ALGORITHMS

The rotate_copy() function(i) Copies rotated range.(ii) Function Parameters : Iterator first, Iterator middle, Iterator last, Iterator result.

An example : #include<iostream> #include<algorithm>

int arr[10]={1,2,3,4,5,6,7,8,9,10};int brr[10];rotate_copy(arr,arr+4,arr+10,brr);

for(a=0;a<10;a++){

cout<<brr[a]<<"\t";}

cout<<endl;

[ Output will be “ 5 6 7 8 9 10 1 2 3 4 5”]

Page 11: STL ALGORITHMS

The reverse() function(i) Reverses the order of the elements in the range [first, last].(ii) Function Parameters : Iterator first, Iterator last.

An example :

#include<iostream> #include<algorithm>

char arr[5]={'a','b','c','d','e'};

reverse(arr,arr+5);for(int a=0;a<5;a++){

cout<<arr[a]<<"\t";}cout<<endl;

[Output will be “e d c b a”]

Page 12: STL ALGORITHMS

Extended Example#include<algorithm>#include<iostream.h>#include<list.h>#include<vector.h>using namespace std;

void main(){

vector <int> v;vector <int> ::iterator it;

v.push_back(10);v.push_back(11);v.push_back(12);v.push_back(13);v.push_back(14);v.push_back(15);

for(it=v.begin();it!=v.end();it++){

cout<<*it<<" ";}

cout<<endl;reverse(v.begin(),v.end());cout<<endl;

for(it=v.begin();it!=v.end();it++){

cout<<*it<<" ";}

}

Page 13: STL ALGORITHMS

The random shuffle() function(i) Rearrange elements in range randomly.(ii) Function Parameters : Iterator first, Iterator last.

An example :

#include<iostream> #include<algorithm>

char arr[5]={'a','b','c','d','e'};random_shuffle(arr,arr+5);for(int a=0;a<5;a++){

cout<<arr[a]<<"\t";}cout<<endl;

[Output can be “e b d c a”]

Page 14: STL ALGORITHMS

Extended Example#include<algorithm>#include<iostream.h>#include<vector.h>using namespace std;void main(){vector <int> v;vector <int> ::iterator it;

v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(6);v.push_back(7);v.push_back(8);v.push_back(9);v.push_back(10);

for(it=v.begin();it!=v.end();it++){cout<<*it<<" ";}

cout<<endl;random_shuffle(v.begin(),v.end());cout<<endl;

for(it=v.begin();it!=v.end();it++){cout<<*it<<" ";}

Page 15: STL ALGORITHMS

The find function(i) This function will as is obvious from it’s name will locate a specified element within the range.(ii) The Function Parameters : First and Last , value.

An example :

#include<iostream> #include<algorithm>

void main(){

char arr[20]="abcdefgh";char *p;p=find(arr,arr+7,'d');puts(p);

}

Page 16: STL ALGORITHMS

The replace function(i) Sets all the elements in the range [first,last) whose current value

equals old_value to a value of new_value.(ii) The Function Parameters : First and Last , old and new value.

An example : #include<iostream> #include<algorithm>

void main(){

char arr[20]="faizansuhail";replace(arr,&arr[10],'a','b');puts(arr);

}

Page 17: STL ALGORITHMS

The fill function(i) Sets value to all elements in the range [first,last). (ii) The Function Parameters : First and Last , value.

An example : #include<iostream> #include<algorithm>

void main(){

int ar[20]={23,45,56,4,45,23};fill(ar,ar+5,45);for(int a=0;a<5;a++)

cout<<"\t"<<ar[a];

}

Page 18: STL ALGORITHMS

The remove function(i) Removes from the range [first,last) the elements with a value equal

to value and returns an iterator to the new end of the range, which

now includes only the values not equal to value. (ii) The Function Parameters : First and Last , value.

An example : #include<iostream> #include<algorithm>

void main(){

char arr[20]="abcdefgh";char *p;p=remove(arr,&arr[7],'h');puts(p);

}

Page 19: STL ALGORITHMS

The search function(i) Searches the range [first1,last1) for the first occurrence of the

sequence defined by [first2,last2), and returns an iterator to its first

element. (ii) The Function Parameters : First1 and Last1 , first2 and last2.

An example : #include<iostream> #include<algorithm>

void main(){

char source[20]="abcdefgh";char tar[20]="fgh",*p;p=search(source,&source[7],tar,&tar[2]);if(*p==source[7]){

cout<<"\nnot found";}else {

puts(p);cout<<"\nfound";

}

}

Page 20: STL ALGORITHMS

The swap function(i) Assigns the content of a to b and the content of b to a (ii) The Function Parameters : a, b

Two objects, whose contents are swapped.

Page 21: STL ALGORITHMS

An example : #include<iostream> #include<algorithm> #include <vector>

void main(){

vector <int> obj;vector <int> tar;

obj.push_back(1);obj.push_back(2);obj.push_back(3);

tar.push_back(4);tar.push_back(5);tar.push_back(6);

swap(obj,tar);

cout<<obj[0];

cout<<obj[1];

cout<<obj[2];

cout<<tar[0];cout<<tar[1];cout<<tar[2];}

}

Page 22: STL ALGORITHMS

The swap ranges function(i) Swaps the values of each of the elements in the

range [first1,last1) with those of their respective elements in the range

beginning at first2. (ii) The Function Parameters : first1, last1 ,first2.

An example : #include<iostream> #include<algorithm> #include <vector>

void main(){

void main(){

int arr[]={1,2,3,4,5,6};int tar[]={6,7,8,9,10,11,12};swap_ranges(arr,arr+6,tar);for(int a=0;a<5;a++){

cout<<"\n"<<arr[a];}

}}

Page 23: STL ALGORITHMS

The count function(i) Returns the number of elements in the range [first,last) that compare

equal to value. (ii) The Function Parameters : first, last.

Page 24: STL ALGORITHMS

An example : #include<iostream> #include<algorithm> #include <vector>

void main(){

int a;int arr[]={1,2,3,4,5,6,7};a=mycount(&arr[0],&arr[7],7);cout<<"\n7 come";cout<<"\t";cout<<a;cout<<"\ttimes\n";cout<<"************************\n";

}

int mycount(int *p,int *q,int b){

int a=0;while(p!=q){

if(*p++==7){

a++;}

}return a;

}

Page 25: STL ALGORITHMS

The minimum function(i) Returns the number of elements in the range [first,last) that compare

equal to value. (ii) The Function Parameters : first, last.

An example : #include<iostream> #include<algorithm> #include <vector>

void main(){

int *a;char *p;int arr[]={1,2,3,4,5,6,7};char tar[]={'a','b','c','d'};a=min_element(arr,arr+7);p=min_element(tar,tar+4);cout<<"\n";cout<<*a;cout<<"\n";cout<<*p;

cout<<"\n";}

Page 26: STL ALGORITHMS

The maximum function(i) Returns the number of elements in the range [first,last) that compare

equal to value. (ii) The Function Parameters : first, last.

An example : #include<iostream> #include<algorithm> #include <vector>

void main(){

int *a;char *p;int arr[]={1,2,3,4,5,6,7};char tar[]={'a','b','c','d'};a=max_element(arr,arr+7);p=max_element(tar,tar+4);cout<<"\n";cout<<*a;cout<<"\n";cout<<*p;

cout<<"\n";}

Page 27: STL ALGORITHMS

The equal function(i) Returns the number of elements in the range [first,last) that compare

equal to value. (ii) The Function Parameters : first, last.

Page 28: STL ALGORITHMS

An example : #include<iostream> #include<algorithm> #include <vector>

void main(){

int arr[]={1,2,3,4,5};int brr[]={1,2,3,4,5};int crr[]={1,2,3,4,1};

if(equal(arr,arr+5,brr)){

cout<<"ok";}

else{cout<<"not ok";}

if(equal(arr,arr+5,crr)){

cout<<"ok";}

else{cout<<"not ok";}

Page 29: STL ALGORITHMS

The for_each() function(i) The for_each function is a very simple function and can be thought of

as an applicator function as it applies a function to the range of the elements.

(ii) Function Parameters : First, last and the function f.An example : #include<iostream> #include<algorithm>

void main(){

myfunction (int i){

i=i+2

}void main()

{int arr[]={1,2,3,4,5};

for_each(arr,arr+5,myfunction);cout<<""<<arr;

}

Page 30: STL ALGORITHMS

The find_if() function(i) The find if function will return the first value in a range on which a

certain function execution yields true.

(ii) Function Parameters : First, last and the predicate.An example : #include<iostream> #include<algorithm>

void main(){

bool iseven(int i){

if(i%2==0)return true;else return false

}void main()

{int arr[]={1,2,3,4,5};

it=find_if(arr,arr+5,iseven);cout<<""<<*it;}

Page 31: STL ALGORITHMS

The binary search() function(i) This function can be thought of as a bool operator and will return true

if the element is found and false if the value is not found.

(ii) Function Parameters : First, last ,value of comp [comparison]

An example : #include<iostream> #include<algorithm>

void main()

{int arr[ ]={1,2,3,4,5};int *it;bool a;

a=binary_search(arr,arr+5,4); cout<<a;}

Page 32: STL ALGORITHMS

The merge function(i) The merge function will connect two ranges of elements.

(ii) Function Parameters: first1, last1,first2,last2,new range. An example : #include<iostream> #include<algorithm>void main(){

int arr[]={1,2,3,4,5};int brr[]={6,7,8,9,10};int crr[10];

merge(arr,arr+5,brr,brr+5,crr);

for(int a=0;a<10;a++){cout<<crr[a];}

}

Page 33: STL ALGORITHMS

The copy function(i) Copies one range of elements into another. WILL OVERWRITE.(ii) Function Parameters: first , last, first Iterator of the range in which it is

to be copied.

An example : #include<iostream> #include<algorithm>void main(){

int arr[ ]={1,2,3,4,5}; int crr[10];

copy (arr,arr+5,crr);

for(int a=0;a<5;a++){cout<<crr[a]<<“”;}

}

Page 34: STL ALGORITHMS

The lexographical function(i) Lexography is the process or work of writing, editing, or compiling a

dictionary. Lexographical_compare returns true if a 1st range is less than a 2nd range lexographicaly.

(ii) Function Parameters: first1,last1,first2,last2.

An example : #include<iostream> #include<algorithm>

void main(){

string a=“pakistan”;string b=“waziristan”;if(lexographical_compare(a, a+7, b, b+9))

cout<<“sab sey pehle Pakistan”<<endl}

Page 35: STL ALGORITHMS

The make_heap function(i) Make_heap is a function that converts a range into a heap. A heap is a binary tree

which satisfies the following conditions:• The largest element is in the root node.• Each element node points progressively to smaller values.

(ii) Function Parameters: first,last.An example : #include<iostream> #include<algorithm>

void main(){

int arr[10]={1,2,3,4,5,6,7,8,9};

for(int a=0;a<10;a++){

cout<<arr[a]<<"\t";}cout<<endl;make_heap(arr, &arr[9]);for(a=0;a<10;a++){

cout<<arr[a]<<"\t";;}

}

Page 36: STL ALGORITHMS

The push_heap function(i) The objective of this function is removing an element from the heap.• What it does is rearrange the elements so that the 1st element is now the last element whereby it is

no longer part of the heap but a range.• To completely remove the element the last element needs to be deleted after pop_heap.

(ii) Function Parameters: first,last.

An example : #include<iostream> #include<algorithm>

void main(){

int arr[10]={1,2,3,4,5,6,7,8,9};make_heap(arr, &arr[9]);arr[9]=31;push_heap(arr, &arr[10]);for(a=0;a<10;a++){

cout<<arr[a]<<"\t";;}

Page 37: STL ALGORITHMS

The pop_heap function(i) The objective of this function is removing an element from the heap.• What it does is rearrange the elements so that the 1st element is now the last element whereby it is

no longer part of the heap but a range.• To completely remove the element the last element needs to be deleted after pop_heap.

(ii) Function Parameters: first,last.

An example : #include<iostream> #include<algorithm>

void main(){

int arr[10]={1,2,3,4,5,6,7,8,9};make_heap (arr, &arr[9]);pop_heap(arr, &arr[8]);for(int a=0;a<10;a++){

cout<<arr[a]<<"\t";;}

Page 38: STL ALGORITHMS

The sort_heap function(i) Rearranges the elements in the heap such that it becomes a sorted

range.(ii) Function Parameters: first,last.

An example : #include<iostream> #include<algorithm>

void main(){

int arr[10]={1,2,3,4,5,6,7,8,9};make_heap(arr, arr[8]);arr[9]=31;push_heap(arr,&arr[9]);for(a=0;a<10;a++){

cout<<arr[a]<<"\t";;}

Page 39: STL ALGORITHMS

Q&A Session