URGENT: How do I use something like a hash_map/hash_table

I need to use something like a hash_table to do the following:

I have custom defined structs that store about users.  As I loop through a ton of data, I need to update those structs frequently, accessing them by their e-mail address (keys) and changing fields in the custom structs.

I then need to loop through every key, struct pair, and spit the data into a xml file.  What can I use to do this type of hashing?  STL hash_map don't seem to be working for me, and I can't find any good documentation.
LVL 5
jjacksnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

burcarpatCommented:
why std::hash_map is not working?  it should be ok for this case

and, if you don't need to parse the xml later on, you can just write the xml file yourself ( easiest and fastest solution although not scalable ).  alternatively, consider using the xerces c++ api from apache ( xml.apache.org ).  finally, you can just write the xml yourself and then parse it using boost spirit ( http://www.boost.org/libs/spirit/index.html )

[ about boost.org :: boost.org is an organization supported by many c++ standards committee members and provides 100% free, peer-reviewed, cross-platform libraries.  many of the boost libraries, such as their smart pointer library, are already in the drafts of the next revision of the c++ standard ]

-- ba
0
bcladdCommented:
What is the problem you are having with hash_map?

I ask because your question seems to be fixated on using hashing. How are your data types defined and how is the hash_map defined? You really want to use someone else's hashing if you can get away with it because while it is fun to implement a hash table, it is also difficult.

-bcl
0
EarthQuakerCommented:
hash_map isn't part of the standard.

You have to download sgi's implementation of the STL to have it ( http://www.sgi.com/tech/stl/ )
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

burcarpatCommented:
yes, hash_map isn't standard ( yet ) but it comes with almost all recent and popular stl implementations.  thus, downloading is not necessary

-- ba
0
jjacksnAuthor Commented:
I don't know how to use hash_maps, I can't find any good documentation.  Maybe you could explain why this is not working correctly:

hash_map<const char*, SalestInfo, hash_compare<const char*, eqstr> > contactList;
      

SalesInfo info = contactList["test@test.com"];
if(info.initalized == true) Show("Incorrect");
else Show("correct");
      
SalesInfo info2;
InitializeSalesInfo(&info2);
contactList["test@test.com"] = info2;

info = contactList["test@test.com"];
if(info.initalized == true) Show("Correct");
else Show("Incorrect");

both times, ContactInfo has not been intialized.
1.  First of all, what does contactList["has not been entered yet"] return?
2.  What am I doing wrong here?
0
jjacksnAuthor Commented:
I have no interest in implementing my own hash table and I can do the XML easily, I just can't seem to figure out the syntax for using the hash table.

I am using VS.NET 2003 with VC7 and just using #include <hash_map> I didn't download anything extra, that I know of.

also, eqstr is:

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
{
       return strcmp(s1, s2) == 0;
}
};
0
burcarpatCommented:
check out this tutorial,

    http://www.rpi.edu/~daiglm/plang/ml3a.html

-- ba
0
AxterCommented:
Try using std::string in your hash_map variable:

Example:

hash_map<std::string, SalestInfo> contactList;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjacksnAuthor Commented:
That is good, but I tried using that and hash is undefined, what do I need to download the SGI STL?
0
AxterCommented:
If you're using VC++ 7.1, hash_map is NOT in the std namespace.

They correctly moved it to stdext namespace instead.

VC++ 7.1 is one of the few compilers/libraries that were smart enough to put the hash_map out side of the std namespace.
0
AxterCommented:
>>what do I need to download the SGI STL?

If you're using VC++ 7.1, you  don't need to download anything.  It comes with the compiler.
Just make sure to use the right namespace.

stdext::hash_map<std::string, SalestInfo> contactList;
0
jjacksnAuthor Commented:
Axter, ok, that compiled, what would

contactList["non existent key"] return?
0
jjacksnAuthor Commented:
Also, the same sample code above running on that list still does not seem to return the object that I am setting into test@test.com
0
AxterCommented:
jjacksn,
You have 27 unlocked questions:
http://oldlook.experts-exchange.com/Programming/Programming_Languages/Cplusplus/viewQuestionHistory.jsp?mid=1168327&orderBy=9&sort=1

Can you please go to the above link, and close out your questions?
0
AxterCommented:
>>contactList["non existent key"] return?  

You need to first set the value, before trying to retreive it.


SalesInfo info2;
InitializeSalesInfo(&info2);
contactList["test@test.com"] = info2;
SalesInfo info_copy = contactList["test@test.com"]; //Now info_copy should get set to the value of info2.
0
AxterCommented:
>>Also, the same sample code above running on that list still does not seem to return the object that I am setting into test@test.com

Please post example code that fails to work.
0
jjacksnAuthor Commented:
ContactInfo info2;
InitializeContactInfo(&info2);   //member bool initalized = true;
contactList["test@test.com"] = info2;
ContactInfo info_copy = contactList["test@test.com"]; //Now info_copy should get set to the value of info2.
//info_copy member bool = false;

It appears it is not giving me back the same object.  

How can I tell if a key has already been used?  If the e-mail address is already in the hashtable, I want to get the object out.  If not, I want to create a new one and insert it.
0
AxterCommented:
>>How can I tell if a key has already been used?  If the e-mail address is already in the hashtable, I want to get the object out.  If
>>not, I want to create a new one and insert it.

Use the hash_map::find member function.  If it returns hash_map::end() then, it's not in the hash_map.

Example:

if (contactList.find("test@test.com") == contactList.end())
{
 //Did not find it
}
else
{
 //Found it
}
0
AxterCommented:
>>It appears it is not giving me back the same object.  

Did you test to see if info2 member is also false?

Do you have a copy constructor and an assignment operator for your ContactInfo class?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.