I am trying to implement a simple comparator for sorting indices based on values in an array "_vec". I am getting an "invalid < operator" run-time error message. I fail to understand what is wrong with the following code:
class Compare{
vector<int>& _vec;
public:
Compare(vector<int>& vec) : _vec(vec) {}
bool operator()(size_t i, size_t j){
if(_vec[i] != _vec[j])
return _vec[i] < _vec[j];
else
return (double)rand()/RAND_MAX < 0.5;
}
};
I am using the following function call:
sort(inds.begin(),inds.end(),Compare(vals));
where inds is just an array containing indices from 1 to 15 (say) and vals is the array of length 15 with some values whose sorted indices I want to compute. The overall goal is to randomize the sort order when two (or more) entries in vals are equal. Any help?