teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2018/10/06 12:22

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -1,5 +1,5 @@
1
- std::find_if で該当する要素を指すイテレータを取得し、
1
+ [std::find_if](https://cpprefjp.github.io/reference/algorithm/find_if.html) で該当する要素を指すイテレータを取得し、
2
- std::distance で先頭要素との距離を取ってやれば良いかと思います。
2
+ [std::distance](https://cpprefjp.github.io/reference/iterator/distance.html) で先頭要素との距離を取ってやれば良いかと思います。
3
3
  ```C++
4
4
  #include <algorithm>
5
5
  #include <iostream>
@@ -22,13 +22,18 @@
22
22
  return row.at(0) == 4 && row.at(1) == 6;
23
23
  }
24
24
  );
25
+ if(it == std::end(data)) {
26
+ std::cout << "not found";
27
+ }
28
+ else {
25
- std::cout << std::distance(std::begin(data), it);
29
+ std::cout << std::distance(std::begin(data), it);
30
+ }
26
31
 
27
32
  return 0;
28
33
  }
29
34
  ```
30
35
 
31
- **実行結果** [Wandbox](https://wandbox.org/permlink/O23KXB5Zc5DilKtL)
36
+ **実行結果** [Wandbox](https://wandbox.org/permlink/GCveUTJfH4yUhHLb)
32
37
  ```
33
38
  0
34
39
  ```