multimap-upper_bound

////////////////////////////////////////
//      2018/05/07 21:35:38
//      multimap-upper_bound

// returns an iterator to the first element greater than a certain value

#include <iostream>
#include <map>
#include <string>
#include <fstream>

using namespace std;

int main(){
    typedef multimap<char, string> M1;
    typedef M1::value_type v_t1;
    M1 m1;

    typedef multimap<string, char, less<string>> M2;
    typedef M2::value_type v_t2;
    M2 m2;

    string word;
    int counter = 0;
    ifstream In(".\\word.txt");

    if (In.good()){
        while (true){
            getline(In, word);
            char ch = word.at(0);
            // file is sorted
            if (ch != 'A' && ch != 'a'){
                break;
            }else{
                // for conting of words
                m1.insert(v_t1(ch,word));
                // for upper_lower bound
                m2.insert(v_t2(word, ch));
            }
        counter++;
        }
        In.close();
    }

    cout << "System Dictionary consists " << counter << " words, with first letter 'a' or 'A'" << endl;

    cout << m1.count('A') << " words starts with 'A'" << endl;
    cout << m1.count('a') << " words starts with 'a'" << endl;

    M2::iterator low = m2.lower_bound("aba");
    M2::iterator upp = m2.upper_bound("abe");

    cout << "Range of the words from 'aba' to 'abe':" << endl;
    while (low != upp){
        cout << low->first << endl;
        low++;
    }
    return 0;
}

/*
OUTPUT:
    System Dictionary consists 61 words, with first letter 'a' or 'A'
    14 words starts with 'A'
    47 words starts with 'a'
    Range of the words from 'aba' to 'abe':
    aback
    abase
    abash
    abate
    abbey
*/ 

word.txt
https://download.csdn.net/download/qwq1503/10387744

posted @ 2018-05-07 22:12  老耗子  阅读(72)  评论(0编辑  收藏  举报