Advertisement
Alaricy

города на первую букву

Jan 16th, 2023 (edited)
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <set>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. template <typename RandomIt>
  10. pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, char prefix) {
  11.     auto begin = range_begin;
  12.     auto end = range_begin;
  13.     bool isstart=true;
  14.     int count_start=0;
  15.     int count_end=0;
  16.     int count=0;
  17.    
  18.     for (auto it1 = range_begin; it1 != range_end; ++it1) {
  19.         string a = *it1;
  20.         if (isstart&&(a[0] == prefix)){
  21.         isstart=false;
  22.         count_start=count;
  23.         count_end=count;
  24.         }
  25.         if (!isstart&&(a[0] == prefix)){
  26.         count_end=count;
  27.         }  
  28.         cout << "s:"<<count_start<<" e:"<<count_end<<" t:"<< count <<endl;
  29.         ++count;
  30.         }
  31.        
  32.         int i=0;
  33.         for (auto it1 = range_begin; it1 != range_end; ++it1){
  34.         if (i<count_start) ++begin;
  35.         if (i<=count_end) ++end;
  36.         ++i;
  37.         }
  38.        
  39.     return {begin, end };
  40. }
  41.  
  42. int main() {
  43.     const vector<string> sorted_strings = {"astrax", "moscow", "murmansk", "mwb", "vologda"};
  44.     const auto m_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'a');
  45.     for (auto it = m_result.first; it != m_result.second; ++it) {
  46.         cout << *it << " ";
  47.     }
  48.     cout << endl;
  49.     const auto p_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'p');
  50.     cout << (p_result.first - begin(sorted_strings)) << " " << (p_result.second - begin(sorted_strings)) << endl;
  51.     const auto z_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'z');
  52.     cout << (z_result.first - begin(sorted_strings)) << " " << (z_result.second - begin(sorted_strings)) << endl;
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement