hash_map not reporting true size when used with hash_comp
I have observed that when we use the hash_comp and define our own
comparison function, hash_map::size() always reports the size of 1,
doesn't matter how many elements we add in the map. We have to use the
hash_comp with user defined comparison function to use the key as
char*.
Plz see the code below - let me know if am wrong any where.
// hash_map_size.cpp
// compile with: /EHsc
#define _DEFINE_DEPRECATED_HASH_CLASSES 0
#include <hash_map>
#include <iostream>
#include <string>
using namespace std;
using namespace stdext;
struct eqint
{
bool operator()(int i1, int i2) const
{
if(i1==i2)
return true ;
else
return false ;
}
};
typedef hash_map<int, int> MYHMAP1;
typedef hash_map<int, int>::size_type MYHMAP_SIZETYPE1;
typedef pair<int, int> Int_Pair;
typedef hash_map<int, int, hash_compare<int, eqint>> MYHMAP2;
typedef hash_map<int, int, hash_compare<int, eqint>>::size_type
MYHMAP_SIZETYPE2;
typedef pair<int, int> Int_Pair;
int main( )
{
// The following code provides the value of the size as expected 1
and 2
// Here I am not using the comparison function in the hash_map
definition
MYHMAP1 phm1;
MYHMAP_SIZETYPE1 i ;
phm1.insert(Int_Pair(1, 1));
i = phm1.size();
cout << "The hash_map length is " << phm1.size() << "." << endl;
phm1.insert(Int_Pair(2, 4));
i = phm1.size();
cout << "The hash_map length is now " << i << "." << endl;
// The folling code doesn't provides the value of the size as
expected 1 and 2
// but it provides the result as 1 and 1
// Here I am using the comparision function in the hash_map
definition
MYHMAP2 phm2;
MYHMAP_SIZETYPE2 j ;
phm2.insert(Int_Pair(1, 1));
j = phm2.size();
cout << "The hash_map length is " << phm2.size() << "." << endl;
phm2.insert(Int_Pair(2, 4));
j = phm2.size();
cout << "The hash_map length is now " << j << "." << endl;
}
Is this bug or I am doing some thing wrong here.
Thanks
-Nilesh