I want to hold a bunch of const char pointers into an std::set container [1]. std::set template requires a comparator functor, and the standard C++ library offers std::less, but its implementation is based on comparing the two keys directly, which is not standard for pointers.
I know I can define my own functor and implement the operator() by casting the pointers to integers and comparing them, but is there a cleaner, ‘standard’ way of doing it?
Please do not suggest creating std::strings – it is a waste of time and space. The strings are static, so they can be compared for (in)equality based on their address.
1: The pointers are to static strings, so there is no problem with their lifetimes – they won’t go away.
Just go ahead and use the default ordering which is less<>. The Standard guarantees that less will work even for pointers to different objects:
‘For templates greater, less, greater_equal, and less_equal, the specializations for any pointer type yield a total order, even if the built-in operators <, >, <=, >= do not.’
The guarantee is there exactly for things like your
set<const char*>.