简体   繁体   中英

Does QMap support custom comparator functions?

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?

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> !
}

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没有这个功能,它需要拥有operator <的关键类型,所以如果你真的需要比较功能,你就会被std :: map困住。

QMap 's key type must provide operator<() . QMap uses it to keep its items sorted, and assumes that two keys x and y are equal if neither x < y nor y < x is true.

In case, overload operator<() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM