Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- template <typename RandomIt>
- pair<RandomIt, RandomIt> FindStartsWith(
- RandomIt range_begin, RandomIt range_end,
- const string& prefix) {
- return equal_range(range_begin, range_end, prefix,
- [](const auto& a, const auto& b) {
- for(auto i = 0; i < a.size(); i++) {
- if(a[i] < b[i])
- return true;
- }
- return false;
- });
- }
- int main() {
- const vector<string> sorted_strings = {"moscow", "motovilikha", "murmansk"};
- const auto mo_result =
- FindStartsWith(begin(sorted_strings), end(sorted_strings), "mo");
- cout << (mo_result.first - begin(sorted_strings)) << " " <<
- (mo_result.second - begin(sorted_strings)) << 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