Advertisement
RobertDeMilo

Поиск в отсортированном векторе, словаре и множестве 2/3

Nov 13th, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. template <typename RandomIt>
  8. pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, char prefix)
  9. {
  10.         auto it1 = lower_bound(range_begin, range_end, string(1, prefix));
  11.         /*auto it2 = upper_bound(range_begin, range_end, string(1, prefix));*/
  12.         auto it2 = upper_bound(range_begin, range_end, prefix,
  13.             [prefix](char str2, const string& str1) {return str1[0] > str2; });
  14.  
  15.         return { it1,it2 };
  16. }
  17.  
  18. int main() {
  19.     const vector<string> sorted_strings = { "moscow", "murmansk", "vologda" };
  20.     const auto m_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'm');
  21.     for (auto it = m_result.first; it != m_result.second; ++it) {
  22.         cout << *it << " ";
  23.     }
  24.     cout << endl;
  25.     const auto p_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'p');
  26.     cout << (p_result.first - begin(sorted_strings)) << " " << (p_result.second - begin(sorted_strings)) << endl;
  27.     const auto z_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'z');
  28.     cout << (z_result.first - begin(sorted_strings)) << " " << (z_result.second - begin(sorted_strings)) << endl;
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement