| | |
Hash in STL
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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.
C++ Syntax (Toggle Plain Text)
#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' ; }
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.
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.

c++ Syntax (Toggle Plain Text)
#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; } }
Last edited by Jessehk; Jul 18th, 2007 at 11:52 pm.
--Jessehk
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
Shouvik
•
•
•
•
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.
See this link for description of functors AKA function objects or functionoid.
Are you Agile.. ?
sorry but i have created it here too
http://www.daniweb.com/forums/thread84373.html
I already saw that and got confused
http://www.daniweb.com/forums/thread84373.html
•
•
•
•
Originally Posted by thekashyap
See this link for description of functors AKA function objects or functionoid.
Regards
Shouvik
Shouvik
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Q: in Link List (Daubly Linked List)
- Next Thread: making a inventory(rpg based)... problems[code] [/code]
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






