0

What I am trying to do, is to create custom string comparator:

struct MyComparator {
    bool operator()(const std::string& lhs, const std::string& rhs) const
    {
        return some_odd_strings_comparison_here;
    }
};

And to use it with unordered map:

std::unordered_map<std::string, T, std::hash<std::string>, MyComparator> some_odd_map 
    = {{...}, {...}, ...};

The final goal is to search through the map:

auto el = some_odd_map.find(smthng);
if (el != some_odd_map.end()) {
    // Found!
}

The problem is that my comparator is never actually get triggered.

UPDATE: Minimal reproducible example

jevgenij
  • 824
  • 1
  • 7
  • 16
  • 7
    If the string hash never compare equal, the equal comparator is never invoked. If you are doing weird string comparisons, you probably need an equally weird hashing function such that strings that compare equal also produce hashes that compare equal. – François Andrieux Aug 26 '22 at 20:01
  • 2
    How do you know? Can we get a [mre] that has that behavior? – NathanOliver Aug 26 '22 at 20:02
  • If you do a find for a string that's in the map, you'll see it do a comparison. For a key that's not found, the bucket might be empty in which case there's no comparison, or it might hash to a bucket that has other key(s), but some implementations (like GCC) store the hash value for inserted keys and can compare that first (no need to scan all the way along an arbitrarily long string to find the last character differs, if you have decent hashes that are easily and cheaply compared). – Tony Delroy Aug 26 '22 at 20:49

0 Answers0