delix777

Спринт 5. Поиск в отсортированном векторе, словаре и множестве. Задание 3.

Feb 2nd, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  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, string prefix) {
  11.     // напишите реализацию
  12.     auto result = equal_range(range_begin,range_end,prefix,[length = prefix.length()](const string& lhs,const string& rhs){return lhs.compare(0,length,rhs, 0, length) < 0;});
  13.     return result;
  14. }
  15. int main() {
  16.     const vector<string> sorted_strings = {"moscow", "motovilikha", "murmansk"};
  17.     const auto mo_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), "mo");
  18.     for (auto it = mo_result.first; it != mo_result.second; ++it) {
  19.         cout << *it << " ";
  20.     }
  21.     cout << endl;
  22.     const auto mt_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), "mt");
  23.     cout << (mt_result.first - begin(sorted_strings)) << " " << (mt_result.second - begin(sorted_strings)) << endl;
  24.     const auto na_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), "na");
  25.     cout << (na_result.first - begin(sorted_strings)) << " " << (na_result.second - begin(sorted_strings)) << endl;
  26.     return 0;
  27. }
Add Comment
Please, Sign In to add comment