01 /*************************/ 02 /* 様々な引数 */ 03 /* coded by Y.Suganuma */ 04 /*************************/ 05 #include <stdio.h> 06 07 bool max1(int x, int y) { 08 return x > y; 09 } 10 11 bool min1(int x, int y) { 12 return x < y; 13 } 14 15 int *min_max(int a, int x[], bool (*blk1)(int, int), auto blk2, int b = 20) { 16 a = 5; 17 x[3] = -3; 18 int *y = new int[3]; 19 20 int re1 = x[0]; 21 for (int i1 = 1; i1 < 5; i1++) 22 re1 = blk1(re1, x[i1]) ? re1 : x[i1]; 23 24 int re2 = x[0]; 25 for (int i1 = 1; i1 < 5; i1++) 26 re2 = blk2(re2, x[i1]) ? re2 : x[i1]; 27 28 y[0] = re1; 29 y[1] = re2; 30 y[2] = a + b; 31 32 return y; 33 } 34 35 int main() 36 { 37 // ラムダ式 max の定義と利用 38 auto max2 = [] (int x, int y) { 39 return x > y; 40 }; 41 printf(" 2 > 1 ? %d\n", max2(2, 1)); 42 // ラムダ式 min の定義と利用 43 auto min2 = [] (int x, int y) { 44 return x < y; 45 }; 46 printf(" 1 < 2 ? %d\n", min2(1, 2)); 47 // 最大値,最小値の計算 48 int a = 10; 49 int x[] = {1, 4, 2, 3, 5}; 50 int *y = min_max(a, x, max1, max2); 51 printf(" max %d %d sum %d a %d x[3] %d\n", y[0], y[1], y[2], a, x[3]); 52 y = min_max(a, x, min1, min2); 53 printf(" min %d %d sum %d a %d x[3] %d\n", y[0], y[1], y[2], a, x[3]); 54 55 return 0; 56 }
2 > 1 ? 1 1 < 2 ? 1 max 5 5 sum 25 a 10 x[3] -3 min -3 -3 sum 25 a 10 x[3] -3