Advertisement
chevengur

СПРИНТ № 5 | Алгоритмы поиска | Урок 4: Поиск в отсортированном векторе, словаре и множестве 2/3

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