Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. template <typename RandomIt>
  9. pair<RandomIt, RandomIt> FindStartsWith(
  10.         RandomIt range_begin, RandomIt range_end,
  11.         const string& prefix) {
  12.     return equal_range(range_begin, range_end, prefix,
  13.             [](const auto& a, const auto& b) {
  14.         for(auto i = 0; i < a.size(); i++) {
  15.             if(a[i] < b[i])
  16.                 return true;
  17.         }
  18.         return false;
  19.     });
  20. }
  21.  
  22. int main() {
  23.     const vector<string> sorted_strings = {"moscow", "motovilikha", "murmansk"};
  24.  
  25.     const auto mo_result =
  26.             FindStartsWith(begin(sorted_strings), end(sorted_strings), "mo");
  27.     cout << (mo_result.first - begin(sorted_strings)) << " " <<
  28.          (mo_result.second - begin(sorted_strings)) << endl;
  29.  
  30.     const auto mt_result =
  31.             FindStartsWith(begin(sorted_strings), end(sorted_strings), "mt");
  32.     cout << (mt_result.first - begin(sorted_strings)) << " " <<
  33.          (mt_result.second - begin(sorted_strings)) << endl;
  34.  
  35.     const auto na_result =
  36.             FindStartsWith(begin(sorted_strings), end(sorted_strings), "na");
  37.     cout << (na_result.first - begin(sorted_strings)) << " " <<
  38.          (na_result.second - begin(sorted_strings)) << endl;
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement