Reputation: 8028
vector< pair<size_t, tuple<double,double> >>
sort_indexes(const vector<tuple<double,double>> &v)
//takes a list and prepends the sorted inxdex
{
// Copy data
vector< pair<size_t, tuple<double,double> >> idx(v.size());
for (size_t i = 0; i != idx.size(); ++i)
{
idx[i].first=i ;
idx[i].second=v[i];
}
sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return get<0>(v[i1]) < get<0>(v[i2]);}
);
return idx;
}
The error looks like:
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\algorithm(3781): error C2664: 'bool sort_indexes::::operator ()(size_t,size_t) const' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'size_t'
I'm confused, what is the form of the comparator? I think that it should be anything that returns a boolean? and the lambda I supplied seams to return a boolean?
When I remove the comparator the code still sorts, although this effect isn't desired as sort by the index has a predictable outcome.
Upvotes: 1
Views: 693
Reputation: 10254
for here, sort
comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less (i.e. is ordered before) the second element.
The signature of the comparison function should be equivalent to the following:
bool cmp(const Type1 &a, const Type2 &b);
The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them.
The type of Type1
and Type2
is pair<size_t, tuple<double,double> >
in your code.
Upvotes: 0
Reputation: 23500
Comparator should be (if sorting by indices):
typedef pair<size_t, tuple<double,double> > param;
sort(idx.begin(), idx.end(), [&v](const param &it, const param &jt) {
return it.first < jt.first;
});
Otherwise:
typedef pair<size_t, tuple<double,double> > param;
sort(idx.begin(), idx.end(), [&v](const param &it, const param &jt) {
return std::get<0>(it.second) < std::get<0>(jt.second);
});
Upvotes: 0
Reputation: 113
simple try it in gcc-4.9, get the below information:
no known conversion for argument 1 from 'std::pair >' to 'size_t {aka long unsigned int}'
simply conclusion: your comp is not correct, you need a comparator of std::pair<size_t, std::tuple<double, double>>
.
Upvotes: 1
Reputation: 27190
You are trying to sort a vector< pair<size_t, tuple<double,double> >>
, so the comparator must compare pair<size_t, tuple<double,double> >
, not size_t
.
Upvotes: 5