为什么会出现这种错误:binary' - ':'std :: _ list_iterator< _mylist>'没有定义此运算符或转换为预定义运算符可接受的类型? [英] Why this error occurs : binary '-' : 'std::_list_iterator<_mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator?
问题描述
你好,我编写了以下代码,用cuples的第二个值(而不是第一个值)对c ++中的地图进行排序。但是,它给了我错误:binary' - ':'std :: _ List_iterator< _Mylist>'没有定义此运算符或转换为预定义运算符可接受的类型并且错误的位置在标题中algorithm.h。它为什么会发生?
hello, I wrote the following code to sort the map in c++ by the second value of tuples(instead of the first value). but, it gives me the error :" binary '-' : 'std::_List_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator" and the position of the error is in the header "algorithm.h". why does it occur?
<pre>template<class T>
struct less_second
: std::binary_function<T,T,bool>
{
inline bool operator()(const T& lhs, const T& rhs)
{
return lhs.second < rhs.second;
}
};
typedef std::pair<int,int> data_t;
std::list<data_t> vec(groups.begin(), groups.end());
std::sort(vec.begin(), vec.end(), less_second<data_t>());
我的尝试:
我使用的代码来自: STL std :: map and sorting using value_compare - General Programming - GameDev.net [ ^ ]
推荐答案
要对列表进行排序,您应该对成员函数进行排序。请参阅该示例: list :: sort - C ++ Reference [ ^ ]。
一般来说,如果你需要排序,你不会使用std :: list
。
如果你是使用地图,然后键是需要排序的字段。您应该重新考虑使用哪些容器,并选择适当的算法。
To sort a list, you should probably take the member function sort. See that example : list::sort - C++ Reference[^].
Generally, you won't use astd::list
if you need sorting.
And if you are using a map, then the key is the field that need to be sorted. You should reconsider which containers you use and also select appropriate algorithms.
这篇关于为什么会出现这种错误:binary' - ':'std :: _ list_iterator< _mylist>'没有定义此运算符或转换为预定义运算符可接受的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!