QMap是否支持自定义比较器函数? [英] Does QMap support custom comparator functions?

查看:590
本文介绍了QMap是否支持自定义比较器函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到为 QMap 设置自定义比较函数的方法,就像我可以为 std :: map typename _Compare = std :: less< _Key> 其部分模板参数)。

I couldn't find a way to set a custom comparator function for QMap, like I can for std::map (the typename _Compare = std::less<_Key> part of its template arguments).

有没有办法设置一个?

Does QMap have a way to set one?

推荐答案

这是一个错误,我认为),但在你可以专门化 qMapLessThanKey 适用于您类型的模板函数(参见)。这将允许你的类型使用一些其他函数,而不是 operator<

It's not documented (and it's a mistake, I think), but in you can specialize the qMapLessThanKey template function for your types (cf. the source). That will allow your type to use some other function rather than operator<:

template<> bool qMapLessThanKey<int>(const int &key1, const int &key2) 
{ 
    return key1 > key2;  // sort by operator> !
}

不过,std :: map的优点是可以指定一个不同的比较器每个地图,而在这里你不能(使用你的类型的所有地图必须看到专业化,否则一切都会崩溃)。

Nonetheless, std::map has the advantage that you can specify a different comparator per each map, while here you can't (all maps using your type must see that specialization, or everything will fall apart).

这篇关于QMap是否支持自定义比较器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆