-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_starts_with_string.cpp
More file actions
38 lines (30 loc) · 1.06 KB
/
Copy pathfind_starts_with_string.cpp
File metadata and controls
38 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
template <typename RandomIt>
pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, const string &prefix)
{
if (range_begin == range_end)
{
return make_pair(range_begin, range_end);
}
auto first = lower_bound(range_begin, range_end, prefix,
[](const typename RandomIt::value_type & a, const typename RandomIt::value_type & b) { return b > a.substr(0, b.size()); });
auto last = upper_bound(range_begin, range_end, prefix,
[](const typename RandomIt::value_type & a, const typename RandomIt::value_type & b) { return a < b.substr(0, a.size()); });
return make_pair(first, last);
}
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;
return 0;
}