Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <set>
- #include <vector>
- using namespace std;
- template <typename RandomIt>
- pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, char prefix) {
- auto begin = range_begin;
- auto end = range_begin;
- bool isstart=true;
- int count_start=0;
- int count_end=0;
- int count=0;
- for (auto it1 = range_begin; it1 != range_end; ++it1) {
- string a = *it1;
- if (isstart&&(a[0] == prefix)){
- isstart=false;
- count_start=count;
- count_end=count;
- }
- if (!isstart&&(a[0] == prefix)){
- count_end=count;
- }
- cout << "s:"<<count_start<<" e:"<<count_end<<" t:"<< count <<endl;
- ++count;
- }
- int i=0;
- for (auto it1 = range_begin; it1 != range_end; ++it1){
- if (i<count_start) ++begin;
- if (i<=count_end) ++end;
- ++i;
- }
- return {begin, end };
- }
- int main() {
- const vector<string> sorted_strings = {"astrax", "moscow", "murmansk", "mwb", "vologda"};
- const auto m_result = FindStartsWith(begin(sorted_strings), end(sorted_strings), 'a');
- 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), 'p');
- 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
Advertisement