0x0
0x0

Reputation: 3075

How to find the size of the hash table?

I've a hash table defined like this

typedef std::unordered_map<unsigned long long int,unsigned long long int> table_map;

and in the program, I read the contents of the a file into a buffer using fread like this:

fread(buffer, sizeof(long long int), 1024, file1);

I declare the hash table as

table_map c1;

Now i create a hash table like

for (i = 0; i < 1024; i++)
    c1.insert(table_map::value_type(buffer[i], i));

Now my questions is, after the for loop how can I get the size of the hash table?

It has 1024 elemts of type unsigned long long int and also the keys of the same type but I could not use sizeof(Mymap) or `size of(c1) beacause its just returning the value 32. Is there any way to find it?

Thanks, Sunil

Upvotes: 3

Views: 7545

Answers (2)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

Multiply the container's size property by the size of a pair:

std::cout << c1.size() * sizeof(table_map::value_type) << "\n";

On my system, this prints out:

16384

This is not totally accurate, because the bookkeeping data isn't accounted for. You cannot account for it, because (as far as I know) the standard doesn't have any guarantees about that implementation detail.

You may get slightly better data if you examine the bucket data. ::bucket, :: bucket_count, ::bucket_size. This may only give you data about keys, values, and pairs though. I haven't tried it.

Upvotes: 4

James McNellis
James McNellis

Reputation: 355039

All of the standard library containers have a size() member function that returns the number of elements in the container.

You can simply call c1.size().

Upvotes: 3

Related Questions