-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArryOfFunctionPointer.cpp
More file actions
51 lines (46 loc) · 975 Bytes
/
Copy pathArryOfFunctionPointer.cpp
File metadata and controls
51 lines (46 loc) · 975 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
using namespace std;
/*
state machine without using any condition and
using from function pointer
*/
bool ascendingCompair(int number1, int number2)
{
if (number1 > number2)
return true;
else
return false;
}
bool descendingCompair(int number1, int number2)
{
if (number1 < number2)
return true;
else
return false;
}
void customSort(vector<int> numbers, bool (*sortFunction)(int, int))
{
for (int i = 0; i < numbers.size(); i++)
{
for (int j = 0; j < numbers.size(); j++)
{
if (sortFunction(numbers[i], numbers[j]))
{
swap(numbers[i], numbers[j]);
}
}
}
for (int i = 0; i < numbers.size(); i++)
{
cout << numbers[i] << endl;
}
}
int main()
{
vector<int> arrayOfIntegers = {77, 3, 4, 6, 32, 1};
bool (*sortFunc)(int, int) = descendingCompair;
customSort(arrayOfIntegers, sortFunc);
customSort(arrayOfIntegers, ascendingCompair);
return 0;
}