Solved

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

Posted on 2003-10-28
19
1,007 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
  • 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
 
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 500 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now