-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path66functors.cpp
More file actions
35 lines (32 loc) · 1.02 KB
/
Copy path66functors.cpp
File metadata and controls
35 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
FUNCTORS OR FUNCTION OBJECTS :
These are functions that behave like objects or functions that are wrapped in the class so that
it is available like an object.
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <functional> // --> header file to include to access the function objects
#include <algorithm> // --> header file to include to access the algorithm functions
using namespace std;
int main()
{
int array[] = {81, 14, 6, 2, 7, 3};
// SORTING ALGORITHM TAKES 2 ARGUMENTS, ONE THE FIRST VALUE AND SECOND THE LAST VALUE
sort(array, array+6);
cout<<"Sorting array in ascending order : "<<endl;
for (int i = 0; i < 6; i++)
{
cout<<array[i]<<" ";
}
// TO SORT IN DESCENDING ORDER, SORTING TAKES 3 ARGUMENTS
cout<<endl<<"Sorting in descending order : "<<endl;
sort(array, array+6, greater<int>());
// greater is a class function
for (int i = 0; i < 6; i++)
{
cout<<array[i]<<" ";
}
// greater is a function object
return 0;
}