C++ 成对向量排序不起作用
我试图按第二个元素对向量对向量进行排序,但它不起作用,而是显示stl_algo.h文件并显示错误。下面是代码。 请忽略我的bits头并使用名称空间标准。我已经阅读了此> 应该是C++ 成对向量排序不起作用,c++,vector,C++,Vector,我试图按第二个元素对向量对向量进行排序,但它不起作用,而是显示stl_algo.h文件并显示错误。下面是代码。 请忽略我的bits头并使用名称空间标准。我已经阅读了此> 应该是 struct sort_pred { bool operator()(pair<pair<int,int>, double> const &l, pair<pair<int,int>, double> const &r) {
struct sort_pred
{
bool operator()(pair<pair<int,int>, double> const &l, pair<pair<int,int>, double> const &r)
{
return l.second<r.second;
}
};
std::sort的第三个参数必须是可调用的。要使结构可调用,需要定义运算符如果使用std::sort,则必须实现比较函数bool func a、B:
但是你不能像以前那样混用它。你能不能更准确地说明你得到的错误。你不认为显示错误会帮助我们帮助你…也请检查一下。你的sort\u pred结构不应该定义运算符吗?你真的不需要所有不必要的代码来显示编译器错误:先生,我使用了你的第二种方法,但仍然打开名为stl_algo.h的文件时出错,我的编译器显示行消息时出错2289错误:调用“sort_pred std::pairSir”时不匹配我正在使用ide的代码块13.12,我已经启用了c++11。非常感谢,先生,它正在工作。我已经按值调用了函数,但没有成功。先生,你能告诉我为什么引用调用不起作用吗?那篇文章是关于std::sort通过引用获取其比较器的,而不是关于比较器通过引用获取其参数的。比较器没有理由不引用它的参数。它们应该是常量引用,但这不是必需的。不幸的是,OP截断他的错误消息对解决他的问题没有多大帮助。
struct sort_pred
{
bool comp(pair< pair<int,int> , double >&l, pair< pair<int,int> , double >&r)
{
return l.second<r.second;
}
};
struct sort_pred
{
bool operator()(pair<pair<int,int>, double> const &l, pair<pair<int,int>, double> const &r)
{
return l.second<r.second;
}
};
bool sort_pred_func( const pair<pair<int,int>,double> &l, const pair<pair<int,int>,double> &r )
{
return l.second < r.second;
}
sort( arr.begin(), arr.end(), sort_pred_func );
struct sort_pred
{
bool operator()( const pair<pair<int,int>,double> &l, const pair<pair<int,int>,double> &r )
{
return l.second < r.second;
}
};
sort(arr.begin(),arr.end(),sort_pred());