Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <iterator>
- #include <algorithm>
- using namespace std;
- bool func(const string& s1, const string& s2){
- if(!s2.size()){
- return 0;
- }
- for(int i=0; i<s2.size(); i++){
- if(s1[i] != s2[i])
- return 0;
- }
- return 1;
- }
- template <typename RandomIt> pair<RandomIt, RandomIt> FindStartsWith(RandomIt range_begin, RandomIt range_end, string prefix){
- string tmp = prefix;
- RandomIt itBeg = lower_bound(range_begin, range_end, tmp);
- RandomIt itEnd = upper_bound (range_begin, range_end, tmp);
- /*
- if(itEnd!=range_end){
- RandomIt tmpIt = itEnd;
- while(tmpIt!= range_end){
- string t = *tmpIt;
- if(t>prefix){
- return (make_pair(itBeg, itEnd));
- }
- tmpIt++;
- }
- }
- if(itBeg==itEnd){
- while(itEnd!=range_end){
- string t = *itEnd;
- if(!func(t, prefix))
- break;
- itEnd++;
- }
- }
- */
- return (make_pair(itBeg, itEnd));
- }
- int main() {
- const vector<string> sorted_strings = {"moscow", "motovilikha", "murmansk", "murmanskk"};
- 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;
- const auto mt_result =
- FindStartsWith(begin(sorted_strings), end(sorted_strings), "mt");
- cout << (mt_result.first - begin(sorted_strings)) << " " <<
- (mt_result.second - begin(sorted_strings)) << endl;
- const auto na_result =
- FindStartsWith(begin(sorted_strings), end(sorted_strings), "murmansk");
- cout << (na_result.first - begin(sorted_strings)) << " " <<
- (na_result.second - begin(sorted_strings)) << endl;
- for (auto it = na_result.first; it != na_result.second; ++it) {
- cout << *it << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement