Advertisement
force1987

??? sprint 5

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