Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-10-28
19
Medium Priority
?
1,023 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +2
19 Comments
 
LVL 7

Expert Comment

by:burcarpat
ID: 9638670
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9638682
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
 
LVL 3

Expert Comment

by:EarthQuaker
ID: 9638689
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:burcarpat
ID: 9638696
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
 
LVL 5

Author Comment

by:jjacksn
ID: 9638698
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
 
LVL 5

Author Comment

by:jjacksn
ID: 9638715
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
 
LVL 7

Expert Comment

by:burcarpat
ID: 9638728
check out this tutorial,

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

-- ba
0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 9638765
Try using std::string in your hash_map variable:

Example:

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

Author Comment

by:jjacksn
ID: 9638768
That is good, but I tried using that and hash is undefined, what do I need to download the SGI STL?
0
 
LVL 30

Expert Comment

by:Axter
ID: 9638796
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
 
LVL 30

Expert Comment

by:Axter
ID: 9638801
>>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
 
LVL 5

Author Comment

by:jjacksn
ID: 9638808
Axter, ok, that compiled, what would

contactList["non existent key"] return?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9638819
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
 
LVL 30

Expert Comment

by:Axter
ID: 9638820
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
 
LVL 30

Expert Comment

by:Axter
ID: 9638835
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 9638840
>>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
 
LVL 5

Author Comment

by:jjacksn
ID: 9638884
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
 
LVL 30

Expert Comment

by:Axter
ID: 9638910
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 9638933
>>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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question