954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hash in STL

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

shouvik.d
Junior Poster
198 posts since Jan 2007
Reputation Points: 64
Solved Threads: 7
 

This one's good.

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 
vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
 

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.

#include <iostream>
#include <functional>

int main( int argc, char** argv )
{
  //using libstdc++ TR1 implementation (g++43 -std=c++0x)
  std::cout << std::hash<char*>()( argv[0] ) << '\n' ;

  //using boost.tr1 library ( #include <boost/tr1/functional.hpp> )
  //std::cout << std::tr1::hash<char*>()( argv[0] ) << '\n' ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

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. :)

#include <string>
#include <iostream>

#include <tr1/unordered_map>

typedef std::tr1::unordered_map<std::string, int> AgeTable;

int main() {
    AgeTable ages;
    
    ages.insert( std::make_pair( "Joe", 25 ) );
    ages.insert( std::make_pair( "Sally", 18 ) );
    ages.insert( std::make_pair( "Billy", 11 ) );
    
    AgeTable::iterator iter;
    for ( iter = ages.begin(); iter != ages.end(); iter++ ) {
        std::cout << iter->first << " is " << iter->second << std::endl;
    }
}
Jessehk
Newbie Poster
20 posts since May 2006
Reputation Points: 33
Solved Threads: 2
 

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.

shouvik.d
Junior Poster
198 posts since Jan 2007
Reputation Points: 64
Solved Threads: 7
 
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.

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

sorry but i have created it here too
http://www.daniweb.com/forums/thread84373.html
See this link for description of functors AKA function objects or functionoid.
I already saw that and got confused :)

shouvik.d
Junior Poster
198 posts since Jan 2007
Reputation Points: 64
Solved Threads: 7
 

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.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You