回答編集履歴

1

修正

2020/05/10 11:36

投稿

episteme
episteme

スコア16612

test CHANGED
@@ -12,6 +12,18 @@
12
12
 
13
13
 
14
14
 
15
+ int binary_search(const std::vector<int>& numbers, int target) {
16
+
17
+ auto itr = std::lower_bound(numbers.begin(), numbers.end(), 5);
18
+
19
+ return (itr == numbers.end() || *itr != 5) ?
20
+
21
+ -1 : static_cast<int>(std::distance(numbers.begin(), itr));
22
+
23
+ }
24
+
25
+
26
+
15
27
  int main(void) {
16
28
 
17
29
  using namespace std;
@@ -20,17 +32,7 @@
20
32
 
21
33
 
22
34
 
23
- auto itr = lower_bound(numbers.begin(), numbers.end(), 5);
24
-
25
- if ( itr == numbers.end() || *itr != 5 ) {
26
-
27
- cout << -1 << endl;
28
-
29
- } else {
30
-
31
- cout << distance(numbers.begin(), itr) << endl;
35
+ cout << binary_search(numbers, 5) << endl;
32
-
33
- }
34
36
 
35
37
  }
36
38