#include <iostream>
#include <array>
#include <functional>
using namespace std;
int func(int x) {
return 5;
}
int goo(int x) {
return 10;
}
bool isEven(const int& number) {
if (number % 2 == 0)return true;
else return false;
}
bool isOdd(const int& number) {
if (number % 2 != 0)return true;
else return false;
}
typedef bool (*check_fcn)(const int&);
using check_fcn_using = bool(*)(const int&);
void printNumbers1(const array<int, 10>& my_array, bool (*check)(const int&)) {
for (auto element : my_array) {
if (check(element))cout << element;
}
}
void printNumbers2(const array<int, 10>& my_array, check_fcn check) {
for (auto element : my_array) {
if (check(element))cout << element;
}
}
void printNumbers3(const array<int, 10>& my_array, check_fcn_using check) {
for (auto element : my_array) {
if (check(element))cout << element;
}
}
void printNumbers4(const array<int, 10>& my_array, std::function<bool(const int&)> check) {
for (auto element : my_array) {
if (check(element))cout << element;
}
}
int main() {
int(*fcnptr)(int) = func;
cout << fcnptr(1) << endl; //5
fcnptr = goo;
cout << fcnptr(1) << endl; //10
///////////////////////////////////////////////////
std::array<int, 10> my_array = { 0,1,2,3,4,5,6,7,8,9 };
printNumbers1(my_array,isEven);
cout << endl;
printNumbers2(my_array, isOdd);
cout << endl;
printNumbers3(my_array, isEven);
cout << endl;
///////////////////////////////////////////////////
std::function<bool(const int&)> fcnptr2 = isEven;
printNumbers4(my_array, fcnptr2);
cout << endl;
fcnptr2 = isOdd;
printNumbers4(my_array, fcnptr2);
return 0;
}
코딩/C++