6.4 Algorithms
6.4 Algorithms
6.4 Algorithms
Algorithms
Algorithms are the functions used across a variety of containers for
processing its contents.
Points to Remember:
Algorithms provide approx 60 algorithm functions to perform the complex
operations.
Standard algorithms allow us to work with two different types of the
container at the same time.
Algorithms are not the member functions of a container, but they are the
standalone template functions.
Algorithms save a lot of time and effort.
If we want to access the STL algorithms, we must include the <algorithm>
header file in our program.
Algorithm find()
#include <iostream>
#include <algorithm>
#include <vector>
int main ()
{
int newints[] = { 50, 60, 70, 80 };
int * q;
q = std::find (newints, newints+4, 60);
if (q != newints+4)
std::cout << "Element found in newints: " << *q << '\n';
else
std::cout << "Element not found in newints\n";
std::vector<int> newvector (newints,newints+4);
std::vector<int>::iterator ti;
ti = find (newvector.begin(), newvector.end(), 60);
if (ti != newvector.end())
std::cout << "Element found in newvector: " << *ti << '\n';
else
std::cout << "Element not found in newvector\n";
return 0;
}
For_Each() Algorithm
#include <vector>
#include <algorithm>
#include <iostream>
void show(int n)
{
cout << n << ” ”;
}
#include <vector>
#include <algorithm>
#include <iostream>
Bool mytest(int n) { return (n>14) && (n <36); };
int arr[] = { 12, 3, 17, 8, 34, 56, 9 }; // standard C array
vector<int> v(arr, arr+7); // initialize vector with C array
int n=count_if(v.begin(),v.end(),mytest); // counts element in v for which
mytest is true
cout << ”found ” << n << ” elements” << endl;
Sorting
#include <iostream>
#include <vector>
#include <algorithm>
int main()
Output
{
vector<int> v = {3, 1, 4, 2, 5};
Before sorting: 3 1 4 2 5
cout<<"Before sorting: "; After sorting: 1 2 3 4 5
for_each(v.begin(), v.end(), [](int x) {
cout << x << " ";
});
sort(v.begin(), v.end());
return 0;
}
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
struct myclass {
bool operator() (int i,int j) { return (i<j);}
Output:
} myobject;
int main () { myvector contains: 12 26 32 33 45 53 71 80
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
return 0;
}
Heap Sort
#include <iostream> // std::cout
#include <algorithm> // std::is_heap, std::make_heap, std::pop_heap
#include <vector> // std::vector
int main () {
vector<int> foo {9,5,2,6,4,1,3,8,7};
if (!is_heap(foo.begin(),foo.end()))
make_heap(foo.begin(),foo.end());
return 0;
}
Algorithm: max()
C++ Algorithm max() function can be used in following 3 ways:
1. It compares the two values passed in its arguments and returns the
larger between them. If both are equal, then it returns the first one.
2. It also compares the two values using a binary function which is
the first one if there are more than one are largest in the list.
Elements are compared using operator < for the first version or using the
given binary comparison function comp for the second version.
max()
#include <algorithm>
#include <iostream> Output:
larger of 1 and 9999: 9999
#include <string> larger of 'a', and 'b': b
longest of "foo", "bar", and "hello": hello
using namespace std;
int main()
{
cout << "larger of 1 and 9999: " << std::max(1, 9999) << '\n'
<< "larger of 'a', and 'b': " << max('a', 'b') << '\n'
<< "longest of \"foo\", \"bar\", and \"hello\": " <<
max( { "foo", "bar", "hello" },
[](const string& s1, const string& s2) {
return s1.size() < s2.size();
}) << '\n';
return 0;
}
Algorithm min()
C++ Algorithm min() function can be used in following 3 ways:
1. It compares the two values passed in its arguments and returns the
smaller between them, and if both are equal, then it returns the first
one.
2. It also compares the two values using a binary function, which is
defined by the user, and then passed as argument in std::min().
3. It is also used to find the smallest element in a given list, and it
returns the first one if there are more than one are smallest in the list.
Elements are compared using operator< for the first version or using the
given binary comparison function comp for the second version.
#include <algorithm>
#include <iostream>
#include <string>
return 0;
}
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune