Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <utility>
- #include <vector>
- using namespace std;
- template <typename RandomIt>
- pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, string prefix) {
- // напишите реализацию
- 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;});
- return result;
- }
- int main() {
- const vector<string> sorted_strings = {"moscow", "motovilikha", "murmansk"};
- const auto mo_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), "mo");
- for (auto it = mo_result.first; it != mo_result.second; ++it) {
- cout << *it << " ";
- }
- cout << endl;
- const auto mt_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), "mt");
- cout << (mt_result.first - begin(sorted_strings)) << " " << (mt_result.second - begin(sorted_strings)) << endl;
- const auto na_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), "na");
- cout << (na_result.first - begin(sorted_strings)) << " " << (na_result.second - begin(sorted_strings)) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement