shizaero

Untitled

May 11th, 2023
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <set>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. template <typename RandomIt>
  8. pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, char prefix) {
  9.     RandomIt it_begin, it_end;
  10.     bool flag = false;
  11.     bool check = false;
  12.  
  13.     for (auto it = range_begin; it < range_end; ++it) {
  14.         if (*((*it).begin()) == prefix && !flag) {
  15.             it_begin = it;
  16.             flag = true;
  17.         }
  18.         if (*((*it).begin()) != prefix && flag) {
  19.             it_end = prev(it);
  20.             flag = false;
  21.             check = true;
  22.         }
  23.     }
  24.     if (check) {
  25.         return make_pair(it_begin, it_end);
  26.     }
  27.     else {
  28.         if ((int)prefix <= (int)(*((*range_begin).begin()))) {
  29.             cout << (int)prefix << " "s << (int)(*((*range_begin).begin())) << endl;
  30.             return make_pair(next(range_begin), next(range_begin));
  31.         }
  32.  
  33.         if ((int)prefix >= (int)(*((*range_end).begin()))) {
  34.             cout << (int)prefix << " "s << (int)(*((*range_end).begin())) << endl;
  35.             return make_pair(prev(range_end), prev(range_end));
  36.         }
  37.  
  38.         for (auto it = range_begin; it < range_end; ++it) {
  39.             if ((int)prefix < (int)(*((*(it + 1)).begin())) &&
  40.                 (int)prefix >(int)(*((*(it)).begin()))) {
  41.                 cout << (int)(*((*it).begin())) << " "s << (int)prefix << " "s << (int)(*((*(it + 1)).begin())) << endl;
  42.                 return make_pair(next(it), next(it));
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. int main() {
  49.     const vector<string> sorted_strings = { "moscow", "murmansk", "vologda", "vorohesh"};
  50.     const auto m_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'm');
  51.     for (auto it = m_result.first; it <= m_result.second; ++it) {
  52.         cout << *it << " ";
  53.     }
  54.     cout << endl;
  55.     const auto a_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'p');
  56.     cout << (a_result.first - begin(sorted_strings)) << " " << (a_result.second - begin(sorted_strings)) << endl;
  57.  
  58.     const auto p_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'a');
  59.     cout << (p_result.first - begin(sorted_strings)) << " " << (p_result.second - begin(sorted_strings)) << endl;
  60.  
  61.     const auto z_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'z');
  62.     cout << (z_result.first - begin(sorted_strings)) << " " << (z_result.second - begin(sorted_strings)) << endl;
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment