Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <set>
- #include <vector>
- #include <iostream>
- using namespace std;
- template <typename RandomIt>
- pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, char prefix) {
- RandomIt it_begin, it_end;
- bool flag = false;
- bool check = false;
- for (auto it = range_begin; it < range_end; ++it) {
- if (*((*it).begin()) == prefix && !flag) {
- it_begin = it;
- flag = true;
- }
- if (*((*it).begin()) != prefix && flag) {
- it_end = prev(it);
- flag = false;
- check = true;
- }
- }
- if (check) {
- return make_pair(it_begin, it_end);
- }
- else {
- if ((int)prefix <= (int)(*((*range_begin).begin()))) {
- cout << (int)prefix << " "s << (int)(*((*range_begin).begin())) << endl;
- return make_pair(next(range_begin), next(range_begin));
- }
- if ((int)prefix >= (int)(*((*range_end).begin()))) {
- cout << (int)prefix << " "s << (int)(*((*range_end).begin())) << endl;
- return make_pair(prev(range_end), prev(range_end));
- }
- for (auto it = range_begin; it < range_end; ++it) {
- if ((int)prefix < (int)(*((*(it + 1)).begin())) &&
- (int)prefix >(int)(*((*(it)).begin()))) {
- cout << (int)(*((*it).begin())) << " "s << (int)prefix << " "s << (int)(*((*(it + 1)).begin())) << endl;
- return make_pair(next(it), next(it));
- }
- }
- }
- }
- int main() {
- const vector<string> sorted_strings = { "moscow", "murmansk", "vologda", "vorohesh"};
- const auto m_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'm');
- for (auto it = m_result.first; it <= m_result.second; ++it) {
- cout << *it << " ";
- }
- cout << endl;
- const auto a_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'p');
- cout << (a_result.first - begin(sorted_strings)) << " " << (a_result.second - begin(sorted_strings)) << endl;
- const auto p_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'a');
- cout << (p_result.first - begin(sorted_strings)) << " " << (p_result.second - begin(sorted_strings)) << endl;
- const auto z_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'z');
- cout << (z_result.first - begin(sorted_strings)) << " " << (z_result.second - begin(sorted_strings)) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment