• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
STL ALGORITHMS
 

STL ALGORITHMS

on

  • 2,583 views

My 2nd semester OOP's and Data Structure presentation.

My 2nd semester OOP's and Data Structure presentation.

STL Algorithms.

Use MS powerpoint to open it.

Statistics

Views

Total Views
2,583
Views on SlideShare
2,580
Embed Views
3

Actions

Likes
1
Downloads
122
Comments
0

1 Embed 3

http://www.slideshare.net 3

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    STL ALGORITHMS STL ALGORITHMS Presentation Transcript

    • 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.
    • What is 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)
      • > 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.
    • 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
    • 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 especially designed to be used on ranges of elements.
      • > Iterators
      • They are a generalization of pointers: they are objects that point to other objects. As the name suggests, iterators are often used to 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 points to the next element.
    • 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
    • The sort() function
      • Sorts the elements in the range [first, last] into ascending order
      • 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]<<&quot; &quot;; } cout <<endl; sort (arr,arr+10); for (a=0;a<10;a++) { cout<<arr[a]<<&quot; &quot;;; } }
    • 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; } }
    • The rotate() function
      • 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.
      • 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]<<&quot; &quot;; } cout <<endl; [ Output will be “ 5 6 7 8 9 10 1 2 3 4 5”]
    • 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]<<&quot; &quot;;
        • }
        • cout <<endl;
      • [ Output will be “ 5 6 7 8 9 10 1 2 3 4 5”]
    • 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]<<&quot; &quot;; } cout <<endl; [Output will be “e d c b a”]
    • 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<<&quot; &quot;; } cout<<endl; reverse(v.begin(),v.end()); cout<<endl; for(it=v.begin();it!=v.end();it++) { cout<<*it<<&quot; &quot;; } }
    • 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]<<&quot; &quot;; } cout <<endl; [Output can be “e b d c a”]
    • 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<<&quot; &quot;; } cout<<endl; random_shuffle(v.begin(),v.end()); cout<<endl; for(it=v.begin();it!=v.end();it++) { cout<<*it<<&quot; &quot;; }
    • 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() { c har arr[20]=&quot;abcdefgh&quot;; char *p; p= find (arr,arr+7,'d'); puts (p); }
    • The replace function
      • 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]=&quot;faizansuhail&quot;; replace (arr,&arr[10],'a','b'); puts (arr); }
    • The fill function
      • 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 <<&quot; &quot;<<ar[a]; }
    • The remove function
      • 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]=&quot;abcdefgh&quot;; char *p; p= remove (arr,&arr[7],'h'); puts (p); }
    • The search function
      • Searches the rang e [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element.
      • (ii) The Function Pa rameters : First1 and Last1 , first2 and last2.
      An example : #include <iostream> #include <algorithm> void main() { char source[20]=&quot;abcdefgh&quot;; char tar[20]=&quot;fgh&quot;,*p; p= search (source,&source[7],tar,&tar[2]); if (*p==source[7]) { cout <<&quot; not found&quot;; } else { puts (p); cout <<&quot; found&quot;; } }
    • The swap function
      • 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 .
    • 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];} }
    • The swap ranges function
      • 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<<&quot; &quot;<<arr[a]; } } }
    • The count function
      • 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; int arr[]={1,2,3,4,5,6,7}; a=mycount(&arr[0],&arr[7],7); cout<<&quot; 7 come&quot;; cout<<&quot; &quot;; cout<<a; cout<<&quot; times &quot;; cout<<&quot;************************ &quot;; } int mycount(int *p,int *q,int b) { int a=0; while (p!=q) { if (*p++==7) { a++; } } return a; }
    • The minimum function
      • 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<<&quot; &quot;; cout<<*a; cout<<&quot; &quot;; cout<<*p; cout<<&quot; &quot;; }
    • The maximum function
      • 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<<&quot; &quot;; cout<<*a; cout<<&quot; &quot;; cout<<*p; cout<<&quot; &quot;;}
    • The equal function
      • 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 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<<&quot;ok&quot;; } else { cout<<&quot;not ok&quot;; } if (equal(arr,arr+5,crr)) { cout<<&quot;ok&quot;; } else { cout<<&quot;not ok&quot;; }
    • The for_each() function
      • 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<<&quot;&quot;<<arr; }
    • The find_if() function
      • 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<<&quot;&quot;<<*it;}
    • The binary search() function
      • 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; }
    • The merge function
      • 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]; } }
    • The copy function
      • 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] <<“” ; } }
    • The lexographical function
      • (i) Lexography is the process or work of writing, editing, or compiling a dictionary. Lexographical_compare returns true if a 1 st range is less than a 2 nd 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 }
    • 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]<<&quot; &quot;; } cout<<endl; make_heap(arr, &arr[9]); for(a=0;a<10;a++) { cout<<arr[a]<<&quot; &quot;;; } }
    • 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]<<&quot; &quot;;; }
    • 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]<<&quot; &quot;;; }
    • 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]<<&quot; &quot;;; }
    • Q&A Session