回答編集履歴

2

追記

2018/01/28 12:06

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -41,3 +41,45 @@
41
41
  ```
42
42
 
43
43
  条件を関数呼び出しにインラインで書けるようになったので可読性も上がります。
44
+
45
+
46
+
47
+ ---
48
+
49
+ 他の使い道としては、条件によって処理の一部分だけ変えたいというときに使うとすっきり書けることがあります。
50
+
51
+ ```C++
52
+
53
+ int sumx(const std::vector<int> &vect, bool odd)
54
+
55
+ {
56
+
57
+ std::function<bool(int)> cond;
58
+
59
+ if(odd)
60
+
61
+ cond = [](int x){return x % 2 != 0;};
62
+
63
+ else
64
+
65
+ cond = [](int x){return x % 2 == 0;};
66
+
67
+
68
+
69
+ int sum = 0;
70
+
71
+ for(auto&& x : vect)
72
+
73
+ {
74
+
75
+ if(cond(x))
76
+
77
+ sum += x;
78
+
79
+ }
80
+
81
+ return sum;
82
+
83
+ }
84
+
85
+ ```

1

余分な文章を削除

2018/01/28 12:06

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  std::sort(vect.begin(), vect.end(), Comp());
28
28
 
29
- ```※コード例なのでstd::greaterを使えという突っ込みは無しでお願いします。
29
+ ```
30
30
 
31
31
 
32
32