fork(1) download
  1. #include <cassert>
  2.  
  3. #include <algorithm>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. template <typename Key, typename Value>
  9. typename std::vector<std::pair<const Key, Value>>::const_iterator
  10. my_find(const std::vector<std::pair<const Key, Value>>& v, const Key& key)
  11. {
  12.  
  13.  
  14. auto it = std::lower_bound(v.begin(), v.end(), key,
  15. [](const std::pair<const Key, Value>& lhs, const Key& rhs) {
  16. return lhs.first < rhs;
  17. }
  18. );
  19. if (it != v.end() && it->first == key) {
  20. return it;
  21. }
  22. return v.end();
  23. }
  24.  
  25. int main()
  26. {
  27. const std::vector<std::pair<const std::string, int>> v = {
  28. {"abc", 42},
  29. {"bar", 51},
  30. {"foo", 8}
  31. };
  32.  
  33. assert(v.begin() + 1 == my_find(v, std::string("bar")));
  34. assert(v.end() == my_find(v, std::string("not present")));
  35.  
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty