shizaero

Untitled

May 11th, 2023
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 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 = range_begin;
  10.     RandomIt it_end = range_end;
  11.     bool check1 = false;
  12.     bool check2 = false;
  13.  
  14.     for (auto it = range_begin; it < range_end; ++it) {
  15.         if (*((*it).begin()) == prefix) {
  16.             it_begin = it;
  17.             check1 = true;
  18.             break;
  19.         }
  20.     }
  21.     for (auto it = prev(range_end); it >= range_begin; --it) {
  22.         if (*((*it).begin()) == prefix) {
  23.             it_end = it;
  24.             check2 = true;
  25.             break;
  26.         }
  27.     }
  28.  
  29.     if (check1 && check2) {
  30.         return make_pair(it_begin, it_end);
  31.     }
  32.     else {
  33.         if ((int)prefix <= (int)(*((*range_begin).begin())))
  34.             return make_pair(next(range_begin), next(range_begin));
  35.  
  36.         if ((int)prefix >= (int)(*((*prev(range_end)).begin())))
  37.             return make_pair(range_end, range_end);
  38.  
  39.         for (auto it = range_begin; it < range_end - 1; ++it) {
  40.             if ((int)prefix < (int)(*((*(it + 1)).begin())) &&
  41.                 (int)prefix >(int)(*((*(it)).begin()))) {
  42.                 return make_pair(next(it), next(it));
  43.             }
  44.         }
  45.     }
  46.     return make_pair(range_begin, range_end);
  47. }
  48.  
  49. int main() {
  50.     const vector<string> sorted_strings = { "moscow", "murmansk", "vologda", "xaoro", "zaero" };
  51.     const auto m_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'm');
  52.     for (auto it = m_result.first; it <= m_result.second; ++it) {
  53.         cout << *it << " ";
  54.     }
  55.     cout << endl;
  56.     const auto p_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'p');
  57.     cout << (p_result.first - begin(sorted_strings)) << " " << (p_result.second - begin(sorted_strings)) << endl;
  58.  
  59.     const auto a_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'a');
  60.     cout << (a_result.first - begin(sorted_strings)) << " " << (a_result.second - begin(sorted_strings)) << endl;
  61.  
  62.     const auto n_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'n');
  63.     cout << (n_result.first - begin(sorted_strings)) << " " << (n_result.second - begin(sorted_strings)) << endl;
  64.  
  65.     const auto z_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'z');
  66.     cout << (z_result.first - begin(sorted_strings)) << " " << (z_result.second - begin(sorted_strings)) << endl;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment