How to sort a multiset in a container by the number of occurrences of elements

I want the elements to be sorted by the number of cases. This is what I came up with (mHeights - std :: multiset):

namespace{
  template<class U,class T>
  class HistPair{
    public:
      HistPair(U count,T const& el):mEl(el),mNumber(count){      
      }
      T const&  getElement()const{return mEl;}

      U getCount()const{return mNumber;}
    private:
      T mEl;
      U mNumber;
  };

  template<class U,class T>
  bool operator <(HistPair<U,T> const& left,HistPair<U,T> const& right){
    return left.getCount()< right.getCount();
  }
}

std::vector<HistPair<int,double>  > calcFrequentHeights(){
  typedef HistPair<int,double> HeightEl;
  typedef std::vector<HistPair<int,double>  > Histogram;
  std::set<double> unique(mHeights.begin(),mHeights.end());
  Histogram res;
  boostForeach(double el, unique) {
    res.push_back(HeightEl(el,mHeights.count(el)));    
  }
  std::sort(res.begin(),res.end());
  std::reverse(res.begin(),res.end()); 
  return res;
}

So, first I take all the unique elements from the multiset, then I count them and sort them into a new container (I need counts, so I use a map). It looks rather complicated for such an easy task. Besides HistPair, which is used in other places, there is no stl algorithm that would simplify this task, for example. using equal_range or sth. So.

Edit: I also need the number of occurrences, sorry I forgot about that

+5
source share
1 answer

, , std::set, std::multiset::count:

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

int main() {
    std::multiset<int> st;
    st.insert(12);
    st.insert(12);
    st.insert(12);
    st.insert(145);
    st.insert(145);
    st.insert(1);
    st.insert(2);

    std::set<int> my_set(st.begin(), st.end());
    std::vector<int> my_vec(my_set.begin(), my_set.end());
    std::sort(my_vec.begin(), my_vec.end(), 
      [&](const int &i1, const int &i2) {
          return st.count(i1) < st.count(i2);
      }
    );

    for(auto i : my_vec) {
        std::cout << i << " ";
    }
    std::cout << std::endl;
}

. :

1 2 145 12

. , , :

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

int main() {
    typedef std::vector<std::pair<int, int>> MyVector;
    std::multiset<int> st;
    st.insert(12);
    st.insert(12);
    st.insert(12);
    st.insert(145);
    st.insert(145);
    st.insert(1);
    st.insert(2);

    std::set<int> my_set(st.begin(), st.end());
    MyVector my_vec;
    my_vec.reserve(my_set.size());

    for(auto i : my_set) 
        my_vec.emplace_back(i, st.count(i));

    std::sort(my_vec.begin(), my_vec.end(), 
      [&](const MyVector::value_type &i1, const MyVector::value_type &i2) {
          return i1.second < i2.second;
      }
    );

    for(const auto &i : my_vec)
        std::cout << i.first << " -> " << i.second << std::endl;
}

:

1 -> 1
2 -> 1
145 -> 2
12 -> 3
+6

All Articles