This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]