adjust associative containers to support heterogeneous lookup #1294

Open
opened 2026-01-30 22:21:29 +00:00 by claunia · 3 comments
Owner

Originally created by @DHowett-MSFT on GitHub (May 24, 2019).

Heterogeneous lookup has been supported since C++14. To quote the linked article,

C++14 allows the lookup to be done via an arbitrary type, so long as the comparison operator can compare that type with the actual key type. This would allow a map from std::string to some value to compare against a const char* or any other type for which an operator< overload is available. It is also useful for indexing composite objects in a std::set by the value of a single member without forcing the user of find to create a dummy object (for example creating an entire struct Person to find a person by name).

Now, it's magic in that you enable it by specifying std::less<> as the third template argument to an associative container; that is:

std::map<std::string, std::string             > map1;
std::map<std::string, std::string, std::less<>> map2;

map2 supports heterogeneous lookup, whereas map1 does not.

Originally created by @DHowett-MSFT on GitHub (May 24, 2019). [Heterogeneous lookup](https://en.wikipedia.org/wiki/C%2B%2B14#Heterogeneous_lookup_in_associative_containers) has been supported since C++14. To quote the linked article, >C++14 allows the lookup to be done via an arbitrary type, so long as the comparison operator can compare that type with the actual key type. This would allow a map from `std::string` to some value to compare against a `const char*` or any other type for which an `operator<` overload is available. It is also useful for indexing composite objects in a `std::set` by the value of a single member without forcing the user of `find` to create a dummy object (for example creating an entire `struct Person` to find a person by name). Now, it's _magic_ in that you enable it by specifying `std::less<>` as the third template argument to an associative container; that is: ```c++ std::map<std::string, std::string > map1; std::map<std::string, std::string, std::less<>> map2; ``` `map2` supports heterogeneous lookup, whereas `map1` does not.
claunia added the Help WantedIssue-TaskProduct-TerminalArea-CodeHealth labels 2026-01-30 22:21:29 +00:00
Author
Owner

@fcharlie commented on GitHub (May 24, 2019):

If you want to use a hash map that supports heterogeneous lookups, there are references below:

@fcharlie commented on GitHub (May 24, 2019): If you want to use a hash map that supports heterogeneous lookups, there are references below: + [absl::flat_hash_map](https://github.com/abseil/abseil-cpp/blob/master/absl/container/flat_hash_map.h) + [parallel-hashmap](https://github.com/greg7mdp/parallel-hashmap)
Author
Owner

@dlong11 commented on GitHub (May 24, 2019):

Are you thinking something like this for stirng keys?

struct StringComparison
{
using is_transparent = void;
bool operator()(std::string_view a, std:string_view b) const
{
return a < b;
}
...
};

std::map<std::string, std::string,, StringComparison> m = <map....>`

@dlong11 commented on GitHub (May 24, 2019): Are you thinking something like this for stirng keys? struct StringComparison { using is_transparent = void; bool operator()(std::string_view a, std:string_view b) const { return a < b; } ... }; std::map<std::string, std::string,, StringComparison> m = <map....>`
Author
Owner

@fcharlie commented on GitHub (May 28, 2019):

@DHowett-MSFT Bartek has written an article that may be helpful to you:
Heterogeneous Lookup in Ordered Containers, C++14 Feature

@fcharlie commented on GitHub (May 28, 2019): @DHowett-MSFT Bartek has written an article that may be helpful to you: [Heterogeneous Lookup in Ordered Containers, C++14 Feature ](https://www.bfilipek.com/2019/05/heterogeneous-lookup-cpp14.html)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#1294