c++ - map::find with a type that's comparable but not convertible to key_type -
std::map::find accepts const key_type& argument. way requires argument convertible key_type. seems oversight me because argument doesn't have convertible key_type, has comparable it.
i can make arbitrary type comparable defining 2 operator< overloads:
struct a; bool operator<(const key_type&, const a&); bool operator<(const a&, const key_type&); map::find should template. it's not. i'm done whining can ask actual question: there way work around , search among map keys comparable not convertible value?
my understanding use std::find_if, linear search instead of binary search (o(n) instead of o(log(n))). naturally, don't want implement binary search hand.
since c++14, map::find in fact template , works desire (at least when given compare::is_transparent condition described below satisfied).
from cppreference:
std::map::find
c++ containers library std::map
iterator find(const key& key ); (1)
const_iterator find( const key& key ) const; (2)
template< class k > iterator find( const k& x ); (3) (since c++14)
template< class k > const_iterator find( const k& x ) const; (4) (since c++14)1,2) finds element key equivalent key.
3,4) finds element key compares equivalent value x. overload participates in overload resolution if qualified-id compare::is_transparent valid , denotes type. allows calling function without constructing instance of key
Comments
Post a Comment