I want to use hash_map with struct values. The code is the following:
#include<stdio.h>
#include<string.h>
#include<hash_map>
using namespace std;
using namespace __gnu_cxx;
struct row {
int number;
char type;
};
struct eqstr {
bool operator()(const char* s1, const char* s2) const {
return (strcmp(s1,s2));
}
};
int main() {
hash_map<const char*, struct row, hash <const char *>, eqstr> columns;
struct row r1,r2;
r1.number=1;
r2.number=2;
r1.type='a';
r2.type='b';
columns["first"]=r1;
columns["second"]=r2;
printf("%i %c\n",columns["first"].number,columns["first"].type);
printf("Columns hash_map size %i\n",columns.size());
hash_map<const char*, struct row, hash <const char *>, eqstr>::iterator it;
it=columns.begin();
for(int i=0;i<columns.size();i++)
{
printf("%s %i %c\n",it->first,it->second.number,it->second.type);
it.operator ++();
}
}
The first printf says columns["first"].number is 0 and columns["first"].char=(null)
It says that the size of the hash_map is 4 (though it has only 2 elements).
The iterator shows me the following ellements:
key row.number row.type
first 0
first 0
first 1 a
second 2 b
What's going on? Thank you.