View difference between Paste ID: 8G5aMfAj and 35F1jgQv
SHOW: | | - or go back to the newest paste.
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
}