multimap-count

////////////////////////////////////////
//      2018/05/02 18:46:10
//      multimap-count

// return the number of elements
#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 count = 0;

    ifstream in(".\word.txt");
    //如果当前流没有发生错误,函数good()返回true ,否则返回false
    if (in.good()){
        while (1){
            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));
            }
            count++;
        }
        in.close();
    }

    cout << "System Dictionary consists " << count
        << " words,
with first letter 'a' or 'A'"
        << endl;
    cout << m1.count('A') << " words start with 'A'"
        << endl;
    cout << m1.count('a') << " words start with 'a'"
        << endl;

    M2::iterator low = m2.lower_bound("aba");
    M2::iterator upp = m2.upper_bound("ace");
    cout << "Range of the words from words 'aba' to 'ace':" << 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 start with 'A'
    47 words start with 'a'
    Range of the words from words 'aba' to 'ace':
    aback
    abase
    abash
    abate
    abbey
    abet
    abhor
    abide
    able
    about
    above
    abuse
    abyss
*/ 

配套word.txt下载
https://download.csdn.net/download/qwq1503/10387744

原文地址:https://www.cnblogs.com/laohaozi/p/12537839.html