:::::::::::::::::::::::::::::::::::::::::source code::::::::::::::::::::::::::
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
using namespace std;
#include <iostream>
#include <sparsehash/dense_hash_map>
using google::dense_hash_map; // namespace where class lives by default
using std::cout;
using std::endl;
using tr1::hash; //__gnu_cxx::hash; //ext::hash; // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS
struct ST_SAVEKEYITEM
{
ST_SAVEKEYITEM()
{
memset(m_szKey, 0x00, sizeof(m_szKey));
memset(m_szValue, 0x00, sizeof(m_szValue));
}
~ST_SAVEKEYITEM()
{
memset(m_szKey, 0x00, sizeof(m_szKey));
memset(m_szValue, 0x00, sizeof(m_szValue));
}
char m_szKey[512];
char m_szValue[8192];
};
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return ((s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0));
}
};
struct StringToIntSerializer {
bool operator()(FILE* fp, const std::pair<const char*, ST_SAVEKEYITEM>& value) const {
if( fwrite(&value.first, sizeof(value.first), 1, fp) != 1 )
return false;
if( fwrite(&value.second, sizeof(value.second), 1, fp) != 1)
return false;
return true;
}
bool operator()(FILE* fp, std::pair<const char*, ST_SAVEKEYITEM>* value) const {
if( fread(&value->first, sizeof(value->first), 1, fp) != 1 )
return false;
if( fread(&value->second, sizeof(value->second), 1, fp) != 1 )
return false;
return true;
}
};
int32_t main( int32_t nArgc, char* pszArrArg[] )
{
ST_SAVEKEYITEM stSaveKey;
dense_hash_map<const char*, ST_SAVEKEYITEM, hash<const char*>, eqstr> months;
months.set_empty_key(NULL);
FILE* fp = fopen("hashtable.data", "r");
months.unserialize(StringToIntSerializer(), fp);
fclose(fp);
/*
months.set_empty_key(NULL);
strcpy(stSaveKey.m_szKey, "january");
strcpy(stSaveKey.m_szValue, "31");
months["january"] = stSaveKey;
strcpy(stSaveKey.m_szKey, "february");
strcpy(stSaveKey.m_szValue, "28");
months["february"] = stSaveKey;
*/
cout << "january key -> " << months["january"].m_szKey << endl;
cout << "january value -> " << months["january"].m_szValue << endl;
cout << "april key -> " << months["april"].m_szKey << endl;
/*
FILE* fp = fopen("hashtable.data", "w");
months.serialize(StringToIntSerializer(), fp);
fclose(fp);
*/
return 0;
}
:::::::::::::::::::::::::::::::::::::compile error message :::::::::::::::::::::::::::::::
# g++ -o test test.cc -I/usr/local/include
In file included from /usr/local/include/sparsehash/dense_hash_map:104,
from test.cc:9:
/usr/local/include/sparsehash/internal/densehashtable.h: In member function ‘bool google::dense_hashtable<Value, Key, HashFcn, ExtractKey, SetKey, EqualKey, Alloc>::unserialize(ValueSerializer, INPUT*) [with ValueSerializer = StringToIntSerializer, INPUT = FILE, Value = std::pair<const char* const, ST_SAVEKEYITEM>, Key = const char*, HashFcn = std::tr1::hash<const char*>, ExtractKey = google::dense_hash_map<const char*, ST_SAVEKEYITEM, std::tr1::hash<const char*>, eqstr, google::libc_allocator_with_realloc<std::pair<const char* const, ST_SAVEKEYITEM> > >::SelectKey, SetKey = google::dense_hash_map<const char*, ST_SAVEKEYITEM, std::tr1::hash<const char*>, eqstr, google::libc_allocator_with_realloc<std::pair<const char* const, ST_SAVEKEYITEM> > >::SetKey, EqualKey = eqstr, Alloc = google::libc_allocator_with_realloc<std::pair<const char* const, ST_SAVEKEYITEM> >]’:
/usr/local/include/sparsehash/dense_hash_map:356: instantiated from ‘bool google::dense_hash_map<Key, T, HashFcn, EqualKey, Alloc>::unserialize(ValueSerializer, INPUT*) [with ValueSerializer = StringToIntSerializer, INPUT = FILE, Key = const char*, T = ST_SAVEKEYITEM, HashFcn = std::tr1::hash<const char*>, EqualKey = eqstr, Alloc = google::libc_allocator_with_realloc<std::pair<const char* const, ST_SAVEKEYITEM> >]’
test.cc:67: instantiated from here
/usr/local/include/sparsehash/internal/densehashtable.h:1156: error: no match for call to ‘(StringToIntSerializer) (FILE*&, std::pair<const char* const, ST_SAVEKEYITEM>*)’
test.cc:41: note: candidates are: bool StringToIntSerializer::operator()(FILE*, const std::pair<const char*, ST_SAVEKEYITEM>&) const
test.cc:49: note: bool StringToIntSerializer::operator()(FILE*, std::pair<const char*, ST_SAVEKEYITEM>*) const
how to modify?????????
Thanks...