/*
    Maps look like vectors, but where vector[..] works for keys being integers 0..N-1 ,
    then a map can use anything (which has a meaningful < as a operation ) as a key.

    Note. vectors are stored sequentially, maps are stored as binary trees. Log2(N) search time.

    Nyhoff p. 655

    http://www.cplusplus.com/reference/map/map/
*/

#include <map>                // map
#include <iostream>           // cin/cout
using namespace std;

int main()
{
    map<char,int> nchars;                     // which means nchars['A'] is a number.

    cout << "Please enter some characters to make a histogram:";

    char c;
    while (cin >> c)
    {
        nchars[c]++;
    }

    if (nchars.empty())
    {
        cout << "No characters entered!"  << endl;
        return 1;
    }

    cout << "Histogram of the characters:" << endl;

    cout << "Iterator access:" << endl;
    for (auto it=nchars.begin();it!=nchars.end();it++)
    {
        cout << it->second << " " << it->first << endl;
    }
    cout << endl;

    cout << "Modern loop access:" << endl;
    for(auto& key_val : nchars) {
        cout << key_val.first << " " << key_val.second << endl;
    }
    cout << endl;

    return 0;
}

