[ create a new paste ] login | about

Link: http://codepad.org/a8lcQ1Od    [ raw code | output | fork ]

C++, pasted on Sep 7:
#include <iostream>
#include <map>
using namespace std;

typedef std::string Str;
typedef std::map<Str,size_t> Data;
typedef Data::const_iterator It;
Data phonebook;

void PrintRange(const Data& book, const Str& range);

int main() {
    
    phonebook["Pupkin"]    = 1111111;
    phonebook["Pupkin1"]   = 1111111;
    phonebook["Pupkin11"]  = 1111111;
    phonebook["Pupkin1f"]  = 1111111;
    phonebook["Pupkin222"] = 1111111;
    phonebook["Pupkin23a"] = 1111111;

    cout<<"contents of phonebook:\n";
    for(It it = phonebook.begin(); it!= phonebook.end(); ++it)
        cout<<"record: "<< it->first<<" : "<<it->second<<endl;

    //--------------
    //ищем все ключи, которые содержат в себе диапазон:
    const Str range = "Pupkin1";
    //--------------

    PrintRange(phonebook, range);

    return 0;
}

void PrintRange(const Data& book, const Str& range)
{
    cout<<"view all the abonents containing part of the name: "<<range<<endl;

    It beg = book.lower_bound(range);
    
    const Str::value_type elem = range[range.length()-1]+1;
    Str end_key = range;
    end_key[end_key.length()-1]=elem;

    const It end = book.lower_bound(end_key);

    It it = beg;
    while(it!=end)
    {
        cout<<"record: "<< it->first<<" : "<<it->second<<endl;
        ++it;
    }

    cout<<"number of abonents : "<<std::distance(beg, end)<<endl;

}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
contents of phonebook:
record: Pupkin : 1111111
record: Pupkin1 : 1111111
record: Pupkin11 : 1111111
record: Pupkin1f : 1111111
record: Pupkin222 : 1111111
record: Pupkin23a : 1111111
view all the abonents containing part of the name: Pupkin1
record: Pupkin1 : 1111111
record: Pupkin11 : 1111111
record: Pupkin1f : 1111111
number of abonents : 3


Create a new paste based on this one


Comments: