-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuncPointers.cpp
52 lines (40 loc) · 992 Bytes
/
FuncPointers.cpp
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
52
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void test(int value) {
cout << "Number: " << value << endl;
}
bool match(string test) {
// return test == "two";
return test.size() == 3;
}
int countStrings(vector<string> &texts, bool (*match)(string test) ) {
int tally = 0;
for(int i=0; i<texts.size(); i++) {
if(match(texts[i])) {
tally++;
}
}
return tally;
}
int main() {
test(5);
void (*pTest)(int) = test;
// & is not required for function
pTest(6);
// * is not required for function
// Using function pointers
vector<string> texts;
texts.push_back("one");
texts.push_back("two");
texts.push_back("three");
texts.push_back("two");
texts.push_back("four");
texts.push_back("two");
texts.push_back("three");
cout << match("one") << endl;
cout << count_if(texts.begin(), texts.end(), match) << endl;
cout << countStrings(texts, match << endl;
return 0;
}