Punchcard

equal_range

Jul 15th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | Source Code | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <set>
  5. #include <vector>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. struct Comp
  11. {
  12.     bool operator()(const string& s, const char& p) const { return !(*begin(s) == p); }
  13.     bool operator()(const char& p, const string& s) const { return !(*begin(s) == p); }
  14.    
  15.     // [](const string& s, const char& p) {return !(*begin(s) == p);}
  16. };
  17.  
  18. template <typename RandomIt>
  19. pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, char prefix) {
  20.     // напишите реализацию
  21.    
  22.  
  23.     auto [b, e] = equal_range(range_begin, range_end, prefix, Comp{} );
  24.    
  25.     return {b, e};
  26.      
  27.  
  28. }
  29.  
  30. int main() {
  31.     const vector<string> sorted_strings = {"moscow", "murmansk", "vologda"};
  32.     const auto m_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'm');
  33.     for (auto it = m_result.first; it != m_result.second; ++it) {
  34.         cout << *it << " ";
  35.     }
  36.     cout << endl;
  37.     const auto p_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'p');
  38.     cout << (p_result.first - begin(sorted_strings)) << " " << (p_result.second - begin(sorted_strings)) << endl;
  39.     const auto z_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'z');
  40.     cout << (z_result.first - begin(sorted_strings)) << " " << (z_result.second - begin(sorted_strings)) << endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment