fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <set>
  5.  
  6. template<class T>
  7. class CompareWords {
  8. public:
  9. bool operator()(T s1, T s2) const
  10. {
  11. if (s1.length() == s2.length())
  12. {
  13. return ( s1 < s2 );
  14. }
  15. else return ( s1.length() < s2.length() );
  16. }
  17. };
  18. typedef std::multiset<std::string, CompareWords<std::string>> mySet;
  19. typedef std::multiset<std::string, CompareWords<std::string>>::iterator mySetItr;
  20.  
  21. void print_rec(const mySet& set, mySetItr it)
  22. {
  23. if (it == set.end()) {
  24. return;
  25. }
  26. const auto& word = *it;
  27. auto next = std::find_if(it, set.end(),
  28. [&word](const std::string& s) {
  29. return s != word;
  30. });
  31. std::cout << word << " appears " << std::distance(it, next) << std::endl;
  32. print_rec(set, next);
  33. }
  34.  
  35. void print(const mySet& set)
  36. {
  37. print_rec(set, set.begin());
  38. }
  39.  
  40. int main (int argc, char* argv[])
  41. {
  42. mySet mWords = {"hello", "world", "Hi", "hello"};
  43.  
  44. print(mWords);
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
Hi appears 1
hello appears 2
world appears 1