Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created October 30, 2015 04:10
Show Gist options
  • Select an option

  • Save ashwin/33d5176e5f0699af2194 to your computer and use it in GitHub Desktop.

Select an option

Save ashwin/33d5176e5f0699af2194 to your computer and use it in GitHub Desktop.
Print buckets and their linked list of keys in C++ unordered container
#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