hashing

The MIT module opens its data structures file with a scan of a hundred hosts Each host has an address, a list of open ports, and an operating system Ordinary variables cannot hold that The module’s answer is nested lists and dictionaries

That is why hashing matters in security work APIs return JSON JSON becomes nested maps If you cannot walk that shape, you cannot automate anything beyond a single request

The map as a lookup table

#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector>

std::string most_common(const std::vector<std::string> &items) {
    std::unordered_map<std::string, int> freq;
    for (const auto &s : items) ++freq[s];
    if (freq.empty()) return "";
    return std::ranges::max_element(freq,
        [](const auto &a, const auto &b) { return a.second < b.second; })->first;
}

On tcp , udp , tcp it returns tcp Average lookup is constant time That fact changes algorithms: “have I seen this before” stops being a scan

Frequency is the first use

Counting is the gateway drug of hashing Once you see it, every problem starts looking like a count

A frequency map answers: which value repeats which value dominates what is missing from the expected set

Nested shapes

struct Host {
    std::string ip;
    std::vector<int> open_ports;
};

std::vector<Host> results{
    {"192.168.1.10", {80, 445}},
    {"192.168.1.20", {22}}
};

for (const auto &host : results)
    for (int port : host.open_ports)
        std::cout << host.ip << ' ' << port << '\n';

A vector of structs, each holding a vector That is the C++ version of the module’s list of dictionaries It is also faster and more explicit about shape

Why the struct wins here

A nested map walks through string keys at runtime A struct knows its fields at compile time The compiler can lay it out and catch a typo before the program runs

The MIT module teaches nested dictionaries because Python needs them C++ gives you a stronger tool Use it

Collisions and honest limits

Hashing is not magic Two keys can land in the same bucket A good hash function spreads them out An attacker who controls keys can force collisions in weaker designs

Standard library implementations are hardened, but do not feed untrusted keys into a home grown hash table and call it secure

Ordering

std::unordered_map does not keep order std::map keeps keys sorted and costs more per operation If you need both lookup and order, say so before choosing

Memory

A map stores keys, values, and table slack For a million small entries that matters A std::unordered_set is cheaper when you only need membership

Mistakes from the module

  • mixing brackets until the shape is wrong
  • asking for a missing key with operator[] and creating it by accident
  • mutating a vector during iteration
  • copying raw personal data into a note because the example needed a value

operator[] inserts a default when the key is missing That is convenient until it quietly changes the thing you were measuring

Exercises

  1. Build a vector of three hosts with two ports each and print every pair
  2. Count status codes in a vector of responses
  3. Replace a linear membership scan with a set and explain the change
  4. Find one JSON response in a tool you use and design a struct for it

What to remember

A map answers “where is this” without walking A vector answers “what order” without guessing Most automation needs both

Posted