|
...こんなもんでどうでしょね。
#include <cstdio>
#include <iostream>
using namespace std;
class Ex1 {
public:
Ex1(){};
~Ex1(){};
int odd(int val) {
return val % 2 != 0;
}
int even(int val) {
return val %2 == 0;
}
int filter(int (Ex1::*func)(int), int *items, int *results, int n) {
int count;
for ( count = 0; 0 < n; --n ) {
if ( (this->*func)(*items) ) {
*results++ = *items;
++count;
}
++items;
}
return count;
}
void Ex1Done() {
int items[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int odds[9] = {0};
int evens[9] = {0};
int i, n;
/* 配列の中身を表示 */
for ( i = 0; i < 9; i++ ) printf("%d ", items[i]);
printf("\n");
/* 偶数のデータのみを取得 */
n = filter(&Ex1::odd, items, odds, 9);
for ( i = 0; i < n; i++ ) printf("%d ", odds[i]);
printf("\n");
/* 奇数のデータのみを取得 */
n = filter(&Ex1::even, items, evens, 9);
for ( i = 0; i < n; i++ ) printf("%d ", evens[i]);
printf("\n");
}
};
int main() {
Ex1 e;
e.Ex1Done();
}
|