Hash in STL

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Hash in STL

 
0
  #1
Jul 18th, 2007
As we all know we don't have Hashes in STL can anybody please suggest an analogous way of emulating it. Or also has any library added it as a standard component?

Please do let me know
Last edited by shouvik.d; Jul 18th, 2007 at 9:02 am.
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Hash in STL

 
0
  #2
Jul 18th, 2007
This one's good.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Hash in STL

 
0
  #3
Jul 18th, 2007
Get STLPort.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Hash in STL

 
0
  #4
Jul 18th, 2007
it is a standard component of the library in c++0x see http://www.open-std.org/jtc1/sc22/wg21/ libstdc++ (gcc 4.30) and dinkumware std c++ library implement this natively; boost.tr1 library could be used if you have an older compiler and boost.
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. int main( int argc, char** argv )
  5. {
  6. //using libstdc++ TR1 implementation (g++43 -std=c++0x)
  7. std::cout << std::hash<char*>()( argv[0] ) << '\n' ;
  8.  
  9. //using boost.tr1 library ( #include <boost/tr1/functional.hpp> )
  10. //std::cout << std::tr1::hash<char*>()( argv[0] ) << '\n' ;
  11. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 19
Reputation: Jessehk is an unknown quantity at this point 
Solved Threads: 2
Jessehk's Avatar
Jessehk Jessehk is offline Offline
Newbie Poster

Re: Hash in STL

 
0
  #5
Jul 18th, 2007
Hash tables (called unordered_map) are part of tr1, which is mostly being implemented for the next version of C++.

GCC has implemented most of tr1 and it happens to include an implementation of unordered_map. Here's an example mapping names (as std::string) to ages (as int).

I am not an expert, so I apologize in advance to the daniweb gurus if this code is incorrect in any way.

  1. #include <string>
  2. #include <iostream>
  3.  
  4. #include <tr1/unordered_map>
  5.  
  6. typedef std::tr1::unordered_map<std::string, int> AgeTable;
  7.  
  8. int main() {
  9. AgeTable ages;
  10.  
  11. ages.insert( std::make_pair( "Joe", 25 ) );
  12. ages.insert( std::make_pair( "Sally", 18 ) );
  13. ages.insert( std::make_pair( "Billy", 11 ) );
  14.  
  15. AgeTable::iterator iter;
  16. for ( iter = ages.begin(); iter != ages.end(); iter++ ) {
  17. std::cout << iter->first << " is " << iter->second << std::endl;
  18. }
  19. }
Last edited by Jessehk; Jul 18th, 2007 at 11:52 pm.
--Jessehk
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: Hash in STL

 
0
  #6
Jul 24th, 2007
while going thru STL I came to know something knows as functors. Thought I understood what practically it is but still could not make out it's advantage and usage. Could anybody throw some light over it. I need to know in detail about it.
Last edited by shouvik.d; Jul 24th, 2007 at 9:12 am.
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Hash in STL

 
0
  #7
Jul 24th, 2007
Originally Posted by shouvik.d View Post
while going thru STL I came to know something knows as functors. Thought I understood what practically it is but still could not make out it's advantage and usage. Could anybody throw some light over it. I need to know in detail about it.
This isn't related to hash, hope you know that. Ideally creation of a new thread for a new topic is helpful for everyone (rather than e.g. discussing all STL doubts in one thread).

See this link for description of functors AKA function objects or functionoid.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: Hash in STL

 
0
  #8
Jul 24th, 2007
sorry but i have created it here too
http://www.daniweb.com/forums/thread84373.html
Originally Posted by thekashyap
See this link for description of functors AKA function objects or functionoid.
I already saw that and got confused
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Hash in STL

 
3
  #9
Jul 24th, 2007
Just use a std::map. You hear me? A std::map. Do you have any good reason to do otherwise? You're probably overestimating the importance of the log n time factor (which hash tables usually still have, albeit in a hidden manner). Of course, maybe you really can't use a std::map for some reason. But I don't see why.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC