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
source
share