Created
October 30, 2015 04:10
-
-
Save ashwin/33d5176e5f0699af2194 to your computer and use it in GitHub Desktop.
Print buckets and their linked list of keys in C++ unordered container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <unordered_set> | |
| #include <iostream> | |
| using IntHashTable = std::unordered_set<int>; | |
| void PrintBuckets(const IntHashTable& htab) | |
| { | |
| for (auto bi = 0; bi < htab.bucket_count(); ++bi) | |
| { | |
| std::cout << "B[" << bi << "]:"; | |
| for (auto it = htab.begin(bi); it != htab.end(bi); ++it) | |
| std::cout << " -> " << *it; | |
| std::cout << "\n"; | |
| } | |
| } | |
| int main() | |
| { | |
| IntHashTable htab = {3, 6, -2, 9, 199, 42}; | |
| PrintBuckets(htab); | |
| return 0; | |
| } | |
| // Output on my computer: | |
| // | |
| // $ ./a.out | |
| // B[0]: -> 42 -> -2 | |
| // B[1]: | |
| // B[2]: -> 9 | |
| // B[3]: -> 199 -> 3 | |
| // B[4]: | |
| // B[5]: | |
| // B[6]: -> 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment