Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <iterator>
- using namespace std;
- template <typename RandomIt>
- pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, char prefix) {
- if (range_begin == range_end)
- return { range_begin, range_end };
- if ((*range_begin)[0] > prefix)
- return{ range_begin,range_begin };
- pair<RandomIt, RandomIt> result;
- auto it = range_begin;
- bool flag = false;
- do {
- if ((*it)[0] == prefix) {
- result.first = it;
- flag = true;
- }
- advance(it, 1);
- } while ((*prev(it))[0] != prefix && it != range_end&& (*it)[0]<=prefix);
- if (flag == false)
- return { it,it };
- flag = false;
- while (it != range_end) {
- advance(it, 1);
- if ((*prev(it))[0] == prefix) {
- result.second = it;
- flag = true;
- }
- if (flag == true)
- return result;
- }
- result.second = next(result.first);
- return result;
- }
- int main() {
- const vector<string> sorted_strings = { "moscow", "murmansk", "vologda" };
- 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 p_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'm');
- 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), 'v');
- cout << (z_result.first - begin(sorted_strings)) << " " << (z_result.second - begin(sorted_strings)) << endl;
- const vector<string> words = {"bara"};
- auto range_begin = words.begin();
- auto range_end = words.end();
- const auto q_result = FindStartsWith(begin(words), end(words), 'a');
- cout << (q_result.first - begin(words)) << " " << (q_result.second - begin(words)) << endl;
- const auto w_result = FindStartsWith(range_begin, range_end, 'b');
- cout << (w_result.first - begin(words)) << " " << (w_result.second - begin(words)) << endl;
- const auto e_result = FindStartsWith(range_begin, range_end, 'c');
- cout << (e_result.first - begin(words)) << " " << (e_result.second - begin(words)) << endl;
- const auto s_result = FindStartsWith(begin(words), end(words), 'z');
- cout << (s_result.first - begin(words)) << " " << (s_result.second - begin(words)) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement