struct CompareCatId : public std::binary_function<Vehicale*, Vehicale*, bool>
{
bool operator()(Vehicle* x, Vehicle* y) const
{
if(x->GetVehicleType() > y->GetVehicleType())
return true;
else if (x->GetVehicleType() == y->GetVehicleType()
&& x>GetLicenseNumber() > y->GetLicenseNumber())
return true;
else
return false;
}
};
这就是我定义我的Set:
的方式 set<Vehicale*,CompareCatId>* m_vehicalesSet;
和ofc我没有忘记包含算法
我尝试使用此行进行排序:
sort(m_vehiclesSet->begin(),m_vehiclesSet->end());
由于某些原因我得到了这个尴尬的错误:
error C2784: 'reverse_iterator<_RanIt>::difference_type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_Tree_const_iterator<_Mytree>'
提前感谢您的帮助。
答案 0 :(得分:6)
当您向其中插入元素时,std::set
会自动排序。您不需要(也不能)手动对其进行排序。
跳过sort(begin, end);
,一切都会好的!
此外,在这种情况下,您的仿函数模仿运算符&lt;,所以您只需编写:
struct CompareCatId : public std::binary_function<Vehicale*, Vehicale*, bool>
{
bool operator()(Vehicle* x, Vehicle* y) const
{
return x->GetVehicleType() < y->GetVehicleType();
}
};
答案 1 :(得分:2)
嘿,我正在尝试对我的集合进行排序ofc我没有忘记包含算法
您是否尝试std::sort
一套?这是毫无意义的,不会起作用
答案 2 :(得分:2)
set是一个已排序的关联容器。一旦创建了集合,就无法对其进行重新排序。排序机制是集合本身的模板参数。
因此,如果您想以不同的顺序表示集合中的元素,则有三个选项:
1)将原始集合中的元素复制到另一个容器(矢量可能最简单),并对其进行排序。
2)使用不同的排序机制重新定义集合
3)创建一个排序索引,以不同的顺序映射对集合的引用。
答案 3 :(得分:0)
std :: set对象是一个有序容器。您可以将排序比较函数指定为第二个模板参数。使用的默认函数对象是std :: less。