SlideShare a Scribd company logo
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;\t&quot;; } cout <<endl; sort (arr,arr+10); for (a=0;a<10;a++) { cout<<arr[a]<<&quot;\t&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;\t&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;\t&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;\t&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;\t&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;\t&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;\nnot found&quot;; } else  { puts (p); cout <<&quot;\nfound&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;\n&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;\n7 come&quot;; cout<<&quot;\t&quot;; cout<<a; cout<<&quot;\ttimes\n&quot;; cout<<&quot;************************\n&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;\n&quot;; cout<<*a; cout<<&quot;\n&quot;; cout<<*p; cout<<&quot;\n&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;\n&quot;; cout<<*a; cout<<&quot;\n&quot;; cout<<*p; cout<<&quot;\n&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;\t&quot;; } cout<<endl; make_heap(arr, &arr[9]); for(a=0;a<10;a++) { cout<<arr[a]<<&quot;\t&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;\t&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;\t&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;\t&quot;;; }
Q&A Session

More Related Content

PDF
SPL 9 | Scope of Variables in C
PPTX
Introduction to Java Strings, By Kavita Ganesan
PPTX
Constructor in java
PDF
Data Structures Notes 2021
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
1 .java basic
PDF
Generics
PPT
Primitive data types in java
SPL 9 | Scope of Variables in C
Introduction to Java Strings, By Kavita Ganesan
Constructor in java
Data Structures Notes 2021
Array Introduction One-dimensional array Multidimensional array
1 .java basic
Generics
Primitive data types in java

What's hot (20)

PPTX
Recursion
PPTX
Pointer in c program
PPT
Java interfaces
PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
C programming - Pointers
PPTX
C Programming: Structure and Union
PDF
Character Array and String
PPT
Java: Primitive Data Types
PPT
Strings
PPTX
Data Structures (CS8391)
PPTX
Operators and expressions in C++
PPTX
Static keyword ppt
PDF
Singly Linked List
PPTX
Insertion in singly linked list
PPTX
Stack using Array
PPTX
Pointer in C
PPTX
Java Notes by C. Sreedhar, GPREC
PPTX
PPTX
Arrays In C++
PPTX
Pointers and Structures.pptx
Recursion
Pointer in c program
Java interfaces
operator overloading & type conversion in cpp over view || c++
C programming - Pointers
C Programming: Structure and Union
Character Array and String
Java: Primitive Data Types
Strings
Data Structures (CS8391)
Operators and expressions in C++
Static keyword ppt
Singly Linked List
Insertion in singly linked list
Stack using Array
Pointer in C
Java Notes by C. Sreedhar, GPREC
Arrays In C++
Pointers and Structures.pptx
Ad

Viewers also liked (20)

PPTX
The Style of C++ 11
PPTX
Idiomatic C++
PDF
Distributed Systems Design
PPTX
Improving The Quality of Existing Software
PPT
Operator overloading
PPT
C++ Advanced
PPTX
Web Service Basics and NWS Setup
PPTX
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
PPT
Operator overloading
PDF
An Introduction to Part of C++ STL
PDF
Solid principles of oo design
PPTX
SOLID Principles part 2
PPT
Programming In C++
PPTX
SOLID Principles part 1
PPT
Exception handling and templates
PPT
Inheritance, polymorphisam, abstract classes and composition)
PPT
Memory Management In C++
PPT
Building Embedded Linux
PPTX
Abstract Base Class and Polymorphism in C++
PPTX
Keys to Continuous Delivery Success - Mark Warren, Product Director, Perforc...
The Style of C++ 11
Idiomatic C++
Distributed Systems Design
Improving The Quality of Existing Software
Operator overloading
C++ Advanced
Web Service Basics and NWS Setup
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Operator overloading
An Introduction to Part of C++ STL
Solid principles of oo design
SOLID Principles part 2
Programming In C++
SOLID Principles part 1
Exception handling and templates
Inheritance, polymorphisam, abstract classes and composition)
Memory Management In C++
Building Embedded Linux
Abstract Base Class and Polymorphism in C++
Keys to Continuous Delivery Success - Mark Warren, Product Director, Perforc...
Ad

Similar to STL ALGORITHMS (20)

PPTX
Object Oriented Design and Programming Unit-05
PPT
Stl Containers
PPTX
Time and Space Complexity Analysis.pptx
PPT
PPT
standard template library(STL) in C++
PPT
Stl (standard template library)
PPTX
Object Oriented Programming Using C++: C++ STL Programming.pptx
PPT
Array Presentation (EngineerBaBu.com)
PPTX
Standard Template Library
ODP
Sysprog 9
PDF
Change the driver file (the main .cpp) so that it asks the user to e.pdf
PPT
Arrays
PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
PPT
Lecture 3 c++
PPT
C chap22
PPTX
C++11 - STL Additions
PPT
Savitch Ch 17
Object Oriented Design and Programming Unit-05
Stl Containers
Time and Space Complexity Analysis.pptx
standard template library(STL) in C++
Stl (standard template library)
Object Oriented Programming Using C++: C++ STL Programming.pptx
Array Presentation (EngineerBaBu.com)
Standard Template Library
Sysprog 9
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Arrays
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Lecture 3 c++
C chap22
C++11 - STL Additions
Savitch Ch 17

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Trump Administration's workforce development strategy
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
master seminar digital applications in india
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Lesson notes of climatology university.
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GDM (1) (1).pptx small presentation for students
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharma ospi slides which help in ospi learning
Trump Administration's workforce development strategy
Weekly quiz Compilation Jan -July 25.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Final Presentation General Medicine 03-08-2024.pptx
master seminar digital applications in india
01-Introduction-to-Information-Management.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
Lesson notes of climatology university.
human mycosis Human fungal infections are called human mycosis..pptx

STL ALGORITHMS

  • 2. 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.
  • 3. 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.
  • 4. 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
  • 5. 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.
  • 6. 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
  • 7. 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;\t&quot;; } cout <<endl; sort (arr,arr+10); for (a=0;a<10;a++) { cout<<arr[a]<<&quot;\t&quot;;; } }
  • 8. 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; } }
  • 9. 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;\t&quot;; } cout <<endl; [ Output will be “ 5 6 7 8 9 10 1 2 3 4 5”]
  • 10. 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;\t&quot;; } cout <<endl; [ Output will be “ 5 6 7 8 9 10 1 2 3 4 5”]
  • 11. 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;\t&quot;; } cout <<endl; [Output will be “e d c b a”]
  • 12. 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;; } }
  • 13. 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;\t&quot;; } cout <<endl; [Output can be “e b d c a”]
  • 14. 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;; }
  • 15. 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); }
  • 16. 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); }
  • 17. 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;\t&quot;<<ar[a]; }
  • 18. 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); }
  • 19. 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;\nnot found&quot;; } else { puts (p); cout <<&quot;\nfound&quot;; } }
  • 20. 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 .
  • 21. 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];} }
  • 22. 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;\n&quot;<<arr[a]; } } }
  • 23. The count function Returns the number of elements in the range [first,last) that compare equal to  value . (ii) The Function Parameters : first, last.
  • 24. 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;\n7 come&quot;; cout<<&quot;\t&quot;; cout<<a; cout<<&quot;\ttimes\n&quot;; cout<<&quot;************************\n&quot;; } int mycount(int *p,int *q,int b) { int a=0; while (p!=q) { if (*p++==7) { a++; } } return a; }
  • 25. 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;\n&quot;; cout<<*a; cout<<&quot;\n&quot;; cout<<*p; cout<<&quot;\n&quot;; }
  • 26. 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;\n&quot;; cout<<*a; cout<<&quot;\n&quot;; cout<<*p; cout<<&quot;\n&quot;;}
  • 27. The equal function Returns the number of elements in the range [first,last) that compare equal to  value . (ii) The Function Parameters : first, last.
  • 28. 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;; }
  • 29. 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; }
  • 30. 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;}
  • 31. 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; }
  • 32. 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]; } }
  • 33. 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] <<“” ; } }
  • 34. 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 }
  • 35. 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;\t&quot;; } cout<<endl; make_heap(arr, &arr[9]); for(a=0;a<10;a++) { cout<<arr[a]<<&quot;\t&quot;;; } }
  • 36. 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;\t&quot;;; }
  • 37. 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;\t&quot;;; }
  • 38. 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;\t&quot;;; }