hash_map
Hi,
My question is kind of off-topic. I am trying to learn hash_map. I have
the below. I expect that I get the following output
---
The key is in there
362:369 10.329
-----
I instead got
--------
362:369 0
---------
Can someone explain me why? What's wrong with my code? I compiled and
ran this code on Debian GNU/Etch with g++ version 4.0.4.
Thanks,
tat
#include <cstdlib>
#include <iostream>
#include <string>
#include <ext/hash_map>
int main(int argc, char* argv[])
{
__gnu_cxx::hash_map<const char*, float,
__gnu_cxx::hash<const char*> > sourceMatrix;
const std::string& p1 = "362:369";
const std::string& p2 = "369:362";
const std::string& p3 = "335:376";
const std::string& p4 = "376:335";
const std::string& p5 = "427:450";
const std::string& p6 = "450:427";
sourceMatrix[p1.c_str()] = sourceMatrix[p2.c_str()] = 10.329;
sourceMatrix[p3.c_str()] = sourceMatrix[p4.c_str()] = 19.142;
sourceMatrix[p5.c_str()] = sourceMatrix[p6.c_str()] = 20.383;
const std::string& key = "362:369";
if (sourceMatrix.find(key.c_str()) != sourceMatrix.end()) {
std::cout << " The key is in there\n";
}
std::cout << key << "\t" << sourceMatrix["362:369"]<< "\n";
return EXIT_SUCCESS;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]