Advertisement
thouxanbanuno

uppBound

Apr 20th, 2021
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <iterator>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8. bool func(const string& s1, const string& s2){
  9.     if(!s2.size()){
  10.         return 0;
  11.     }
  12.     for(int i=0; i<s2.size(); i++){
  13.         if(s1[i] != s2[i])
  14.             return 0;
  15.     }
  16.     return 1;
  17. }
  18. template <typename RandomIt> pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, string prefix){
  19.     string tmp = prefix;
  20.     RandomIt itBeg = lower_bound(range_begin, range_end, tmp);
  21.     RandomIt itEnd = upper_bound (range_begin, range_end, tmp);
  22.     /*
  23.     if(itEnd!=range_end){
  24.         RandomIt tmpIt = itEnd;
  25.         while(tmpIt!= range_end){
  26.             string t = *tmpIt;
  27.             if(t>prefix){
  28.                 return (make_pair(itBeg, itEnd));
  29.             }
  30.             tmpIt++;
  31.         }
  32.     }
  33.     if(itBeg==itEnd){
  34.         while(itEnd!=range_end){
  35.             string t = *itEnd;
  36.             if(!func(t, prefix))
  37.                 break;
  38.             itEnd++;
  39.         }
  40.     }
  41.     */
  42.     return (make_pair(itBeg, itEnd));
  43. }
  44.  
  45. int main() {
  46.   const vector<string> sorted_strings = {"moscow", "motovilikha", "murmansk", "murmanskk"};
  47.  
  48.   const auto mo_result =
  49.       FindStartsWith(begin(sorted_strings), end(sorted_strings), "mo");
  50.   for (auto it = mo_result.first; it != mo_result.second; ++it) {
  51.     cout << *it << " ";
  52.   }
  53.   cout << endl;
  54.  
  55.   const auto mt_result =
  56.       FindStartsWith(begin(sorted_strings), end(sorted_strings), "mt");
  57.   cout << (mt_result.first - begin(sorted_strings)) << " " <<
  58.       (mt_result.second - begin(sorted_strings)) << endl;
  59.  
  60.   const auto na_result =
  61.       FindStartsWith(begin(sorted_strings), end(sorted_strings), "murmansk");
  62.   cout << (na_result.first - begin(sorted_strings)) << " " <<
  63.       (na_result.second - begin(sorted_strings)) << endl;
  64.     for (auto it = na_result.first; it != na_result.second; ++it) {
  65.     cout << *it << " ";
  66.   }
  67.   cout << endl;
  68.  
  69.   return 0;
  70. }
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement