• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

Hash map flexibility refers to the ability of std::unordered_map (and similar hash-based associative containers) to accept any hashable type as a key — including char, int, string, long long, and custom types with a user-provided hash function. This contrasts with frequency arrays, which are limited to integer-indexable domains.

Explanation

A frequency array is hardcoded to a specific domain: int freq[26] works only for 26 lowercase letters. A hash map, by contrast, works for any type that has a hash function. Need to count word frequencies? unordered_map<string, int>. Need to count frequencies of long long IDs? unordered_map<long long, int>. The same data structure and the same freq[key]++ pattern works across all key types.

How It Works

  1. C++ std::unordered_map uses std::hash<Key> to compute a size_t hash value for any key
  2. The C++ standard library provides specializations of std::hash for all fundamental types
  3. At insertion, the key is stored alongside the value in the bucket
  4. On lookup, the key is hashed again, and the bucket chain is compared using operator==
  5. For custom types, the programmer provides a std::hash specialization

Visual Explanation

map_flexibility MAP std::unordered_map<Key, int> CHAR Key = char freq['a']++ MAP->CHAR STR Key = string freq["hello"]++ MAP->STR INT Key = int freq[42]++ MAP->INT CUSTOM Key = UserType (custom hash) MAP->CUSTOM

Semantic Network

semantic_hash_map_flexibility THIS Hash Map Flexibility PRE1 Hash Collision Overhead THIS--PRE1 built from OUT1 Unordered Map Frequency THIS--OUT1 builds into OUT2 ASCII Math Elimination THIS--OUT2 builds into CON1 Known Range Assumption THIS--CON1 contrasts with CON2 Memory Efficiency Array THIS--CON2 contrasts with REL1 Unordered Map Non-Determinism THIS--REL1 related REL2 Array vs Hash Map Tradeoffs THIS--REL2 related

Key Properties

  • Works with any hashable type: char, int, string, long long, pointers, custom structs
  • No compile-time domain constraint — the key type is a template parameter, not a fixed size
  • Same code pattern (freq[key]++) regardless of key type
  • Custom types require a custom hash function and operator==
  • The flexibility comes at a cost: hash computation, dynamic memory allocation, pointer indirection

Connections

Edge Cases & Gotchas

  • std::unordered_map has no default hash for custom types — the compiler error is cryptic (“cannot convert from T to size_t”)
  • For string keys, the hash function iterates the entire string — O(len(key)) per hash, not just O(1)
  • Floating-point keys are problematic: NaN != NaN per IEEE 754, so lookup fails
  • Pointer keys hash by address, not by value — two different pointers with the same value are different keys
  • The flexibility argument cuts both ways: too flexible means type errors surface at runtime or as linker errors