//usealgo.cpp
//#pragma warning (disable : 4786) // useful for MSVC++ 5.0
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>
#include <cctype>
using namespace std;

string & ToLower(string & st);

int main()
{
    vector<string> words;
    cout << "Enter words (enter quit to quit):\n";
    string input;
    while (cin >> input && input != "quit")
        words.push_back(input);

    cout << "You entered the following words:\n";
    ostream_iterator<string,char> out(cout, " ");
    copy(words.begin(), words.end(), out);
    cout << endl;
   
    // place words in set, converting to lower case
    set<string> wordset;
    transform(words.begin(), words.end(),
        insert_iterator<set<string> > (wordset, wordset.begin()),
        ToLower);
    cout << "\nAlphabetic list of words:\n";
    copy(wordset.begin(), wordset.end(), out);
    cout << endl;

    // place word and frequency in map
    map<string, int> wordmap;
    set<string>::iterator  si;
    for (si = wordset.begin(); si != wordset.end(); si++)
         wordmap[*si] = count(words.begin(), words.end(), *si);

    // display map contents
    cout << "\nWord frequency:\n";
    for (si = wordset.begin(); si != wordset.end(); si++)
        cout << *si << ": " << wordmap[*si] << endl;
 
    return 0;
}

string & ToLower(string & st)
{
    transform(st.begin(), st.end(), st.begin(), tolower);
    return st;
}
