#include <algorithm>
#include <iostream>
#include <string>
#include <set>

template<class T>
class CompareWords {
public:
    bool operator()(T s1, T s2) const
    {
        if (s1.length() == s2.length())
        {
            return ( s1 < s2 );
        }
        else return ( s1.length() < s2.length() );
    }
};
typedef std::multiset<std::string, CompareWords<std::string>> mySet;
typedef std::multiset<std::string, CompareWords<std::string>>::iterator mySetItr;

void print_rec(const mySet& set, mySetItr it)
{
    if (it == set.end()) {
        return;
    }
    const auto& word = *it;
    auto next = std::find_if(it, set.end(),
        [&word](const std::string& s) {
            return s != word;
        });
    std::cout << word << " appears " << std::distance(it, next) << std::endl;
    print_rec(set, next);
}

void print(const mySet& set)
{
    print_rec(set, set.begin());
}

int main (int argc, char* argv[])
{
    mySet mWords = {"hello", "world", "Hi", "hello"};

    print(mWords);
    return 0;
}
