[go: up one dir, main page]

0% found this document useful (0 votes)
13 views15 pages

6.4 Algorithms

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

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

int arr[] = { 12, 3, 17, 8 }; // standard C array


vector<int> v(arr, arr+4); // initialize vector with C array
for_each (v.begin(), v.end(), show); // apply function show
// to each element of vector v
Find() Algorithm
#include <vector>
#include <algorithm>
#include <iostream>
int key;
int arr[] = { 12, 3, 17, 8, 34, 56, 9 }; // standard C array
vector<int> v(arr, arr+7); // initialize vector with C array
vector<int>::iterator iter;
cout << ”enter value :”;
cin >> key;
iter=find(v.begin(),v.end(),key); // finds integer key in v
if (iter != v.end()) // found the element
cout << ”Element ” << key << ” found” << endl;
else
cout << ”Element ” << key << ” not in vector v” << endl;
Find_If() Algorithm
#include <vector>
#include <algorithm>
#include <iostream>
Bool mytest(int n) { return (n>21) && (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
vector<int>::iterator iter;
iter=find_if(v.begin(),v.end(),mytest);
// finds element in v for which mytest is true
if (iter != v.end()) // found the element
cout << ”found ” << *iter << endl;
else
cout << ”not found” << endl;
Count_If() Algorithm

#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>

using namespace std;

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

cout<<"\nAfter sorting: ";


for_each(v.begin(), v.end(), [](int x) {
cout << x << " ";
});

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)

// print out content:


cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';

return 0;
}
Heap Sort
#include <iostream> // std::cout
#include <algorithm> // std::is_heap, std::make_heap, std::pop_heap
#include <vector> // std::vector

using namespace std;

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

cout << "Popping out elements:";


while (!foo.empty()) { Output:
pop_heap(foo.begin(),foo.end()); // moves largest element to back Popping out elements: 9 8 7 6 5 4 3 2 1
cout << ' ' << foo.back(); // prints back
foo.pop_back(); // pops element out of container
}
cout << '\n';

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

defined by the user, and then passed as an argument in std::max().


3. It is also used to find the largest element in a given list, and it returns

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>

using namespace std; Output:


smaller of 1 and 9999: 1
smaller of 'a', and 'b': a
int main() shortest of "foo", "bar", and "hello": foo
{
cout << "smaller of 1 and 9999: " << min(1, 9999) << '\n'
<< "smaller of 'a', and 'b': " << min('a', 'b') << '\n'
<< "shortest of \"foo\", \"bar\", and \"hello\": " <<
min( { "foo", "bar", "hello" },
[](const string& s1, const string& s2) {
return s1.size() < s2.size();
}) << '\n';

return 0;
}
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune

You might also like