This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Is this normal behavior or an error [libstdc++/has_map]
- From: Paolo Carlini <pcarlini at suse dot de>
- To: eschenb at cs dot uni-frankfurt dot de
- Cc: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 06 Aug 2004 12:46:46 +0200
- Subject: Re: Is this normal behavior or an error [libstdc++/has_map]
Hi,
answer: it is an error... on your part ;)
Basically, you are misusing the hashed container. The below, modeled after
http://www.sgi.com/tech/stl/hash_map.html
works.
Ciao,
Paolo.
///////////////////
#include <ext/hash_map>
#include <string>
#include <iostream>
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
int main()
{
__gnu_cxx::hash_map<const char *, int, __gnu_cxx::hash<const char *>,
eqstr> testmap;
__gnu_cxx::hash<const char *> Hash;
std::string dummy;
testmap["test"]=7;
std::cout << "Output of testmap[\"test\"]: " << testmap["test"] <<
std::endl;
dummy="test";
std::cout << "Output of testmap[dummy.c_str()]: " <<
testmap[dummy.c_str()] << std::endl;
std::cout << "Hash(\"test\"): " << Hash("test") << std::endl;
std::cout << "Hash(dummy.c_str(): " << Hash(dummy.c_str()) << std::endl;
return 0;
}