[英]Comparator for std::algorithm::sort?
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' 1> C:\\ Program Files(x86)\\ Microsoft Visual Studio 11.0 \\ VC \\ include \\ algorithm(3781):错误C2664:'bool sort_indexes :::: operator()(size_t,size_t)const':无法转换参数1从'std :: pair <_Ty1,_Ty2>'到'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? 和我提供的lambda接缝返回布尔值?
When I remove the comparator the code still sorts, although this effect isn't desired as sort by the index has a predictable outcome. 当我删除比较器时,代码仍会进行排序,尽管由于按索引进行排序具有可预期的结果,所以并不需要这种效果。
您正在尝试对vector< pair<size_t, tuple<double,double> >>
进行排序,因此比较器必须比较pair<size_t, tuple<double,double> >
,而不是size_t
。
simple try it in gcc-4.9, get the below information: 只需在gcc-4.9中尝试一下,即可获得以下信息:
no known conversion for argument 1 from 'std::pair >' to 'size_t {aka long unsigned int}' 没有将参数1从'std :: pair>'转换为'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>>
. 简单的结论就是:您的comp不正确,您需要std::pair<size_t, std::tuple<double, double>>
。
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);
});
comp - comparison function object (ie an object that satisfies the requirements of Compare) which returns true if the first argument is less (ie is ordered before) the second element. comp-比较函数对象(即满足Compare要求的对象),如果第一个参数小于第二个元素(即在第二个元素之前排序),则返回true。
The signature of the comparison function should be equivalent to the following: 比较函数的签名应等效于以下内容:
bool cmp(const Type1 &a, const Type2 &b); 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. 签名不需要具有const&,但是函数对象不得修改传递给它的对象。 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. Type1和Type2类型必须使得可以取消引用RandomIt类型的对象,然后将其隐式转换为它们两者。
The type of Type1
and Type2
is pair<size_t, tuple<double,double> >
in your code. Type1
和Type2
的类型在代码中为pair<size_t, tuple<double,double> >
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.