D
digz
This is a simple case of a problem I am having with using char* in a
hash_map,
The code prints the size correctly( 3) and it should print three lines
of output but is printing only two. I do not want to reserve space for
the char* using new etc..( it will never will be deepcopied using
strcpy etc..) ..it always holds an address.
I know I am making some very fundamental mistake what am I missing
here ?
#include<ext/hash_map>
#include<boost/functional/hash.hpp>
#include<iostream>
using namespace std;
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
struct cacheType {
int i;
double d;
char* c;
cacheType():i(0),d(0.0),c(0){}
};
int main()
{
typedef boost::hash<const char*> strHashFn;
typedef __gnu_cxx::hash_map<const char*, cacheType, strHashFn,
eqstr> cacheMap;
typedef cacheMap::const_iterator It;
cacheMap cMap_;
cacheType ci_, cd_, cc_;
ci_.i = 1;
cd_.d = 2.0;
const char* c = "2007-09-11 18:00:00";
cc_.c = const_cast<char*>(c);
cMap_.insert(make_pair("A2", cc_));
cMap_.insert(make_pair("A1", cd_));
cMap_.insert(make_pair("A0", ci_));
cout << cMap_.size() << std::endl;
for( It i=cMap_.begin(); i != cMap_.end(); i++)
cout << i->first << " " << (i->second).i << " " << (i->second).d
<< " "<
< (i->second).c << std::endl;
}
Thanks
Digz
hash_map,
The code prints the size correctly( 3) and it should print three lines
of output but is printing only two. I do not want to reserve space for
the char* using new etc..( it will never will be deepcopied using
strcpy etc..) ..it always holds an address.
I know I am making some very fundamental mistake what am I missing
here ?
#include<ext/hash_map>
#include<boost/functional/hash.hpp>
#include<iostream>
using namespace std;
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
struct cacheType {
int i;
double d;
char* c;
cacheType():i(0),d(0.0),c(0){}
};
int main()
{
typedef boost::hash<const char*> strHashFn;
typedef __gnu_cxx::hash_map<const char*, cacheType, strHashFn,
eqstr> cacheMap;
typedef cacheMap::const_iterator It;
cacheMap cMap_;
cacheType ci_, cd_, cc_;
ci_.i = 1;
cd_.d = 2.0;
const char* c = "2007-09-11 18:00:00";
cc_.c = const_cast<char*>(c);
cMap_.insert(make_pair("A2", cc_));
cMap_.insert(make_pair("A1", cd_));
cMap_.insert(make_pair("A0", ci_));
cout << cMap_.size() << std::endl;
for( It i=cMap_.begin(); i != cMap_.end(); i++)
cout << i->first << " " << (i->second).i << " " << (i->second).d
<< " "<
< (i->second).c << std::endl;
}
Thanks
Digz