C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 12th, 2003, 02:14 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,100 Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 8 h 50 m 41 sec
Reputation Power: 2059
Tip about STL hash_map and string

This is not really a question, but a tip for others who may possibly search the forum for this particular issue:

The problem:
This code works:
Code:
#include <iostream>
#include <hash_map>
using namespace std;

hash_map <const char *, int> months;
int main(void) {
   months["january"] = 31;
   months["february"] = 28;
   return 0;
}


However, this code doesn't compile:
Code:
#include <iostream>
#include <string>
#include <hash_map>
using namespace std;

hash_map <string, int> months;
int main(void) {
   months["january"] = 31;
   months["february"] = 28;
   return 0;
}

Note that the only difference between the two codes is that one uses const char * and the other uses string for the hash key type. So why does the second instance not compile?

Background: What is a hash_map?
The STL library provides a template type called map which allows a C++ programmer to create an associative array (i.e). array[key] = value;
However, a map template automatically orders its elements as they're inserted, therefore lookups are a little slower. A hash_map is a template type that allows a programmer to quickly look up a value. The hash_map template uses a hashing algorithm to store values that are inserted into it, hence the elements are NOT sorted. This is OK, if you just want to do quick in-memory lookups, but don't care about the data being sorted. The map type is part of the STL, but hash_map was introduced by SGI and is present in only some implementations of the STL (M$ VC++ 6.0 and .NET do not have it in their default installation). However, two of the well known implementations from SGI (http://www.sgi.com/tech/stl/index.html) and stlport (http://www.stlport.org/) do and can be used as drop in replacements, if your compiler does not support hash maps. See http://www.sgi.com/tech/stl/table_of_contents.html for documentation and sample code using hash_map .

The reason it doesn't compile:
Since hash_map is not part of the standard STL, there are certain other parts of the STL that it doesn't work with (notably the string template, which is pretty much standard across all STL implementations). You need to supply some extra code yourself, for the hash function to work correctly with other parts of the STL, which are standard across all implementations.

The solution:
Add the following chunk of code between the BEGIN FIX and END FIX comments:
Code:
#include <iostream>
#include <string>
#include <hash_map>
using namespace std;

/** BEGIN FIX **/
namespace std                                                                                 
{                                                                                             
  template<> struct hash< std::string >                                                       
  {                                                                                           
    size_t operator()( const std::string& x ) const                                           
    {                                                                                         
      return hash< const char* >()( x.c_str() );                                              
    }                                                                                         
  };                                                                                          
}          
/** END FIX **/

hash_map <string, int> months;
int main(void) {
   months["january"] = 31;
   months["february"] = 28;
   return 0;
}


I tested this code on the following OS/compiler environments:
Red Hat Linux 7.1: gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)
FreeBSD 4.7-RELEASE-p7:gcc version 2.95.4 20020320 [FreeBSD]
OpenBSD 3.3-CURRENT:gcc version 2.95.3 20010125 (prerelease, propolice)

[edit] Note that I was using the STL implementations that came by default with the distros and they all supported hash_map [/edit]

Hope this helps!
Comments on this post
sizablegrin agrees!

Last edited by Scorpions4ever : March 12th, 2003 at 06:11 PM.

Reply With Quote
  #2  
Old August 11th, 2008, 05:16 PM
complete's Avatar
complete complete is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 121 complete User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 54 m 53 sec
Reputation Power: 0
According to this post, all I need to include in order to use Hash Tables is:

Quote:
#include <iostream>
#include <hash_map>

using namespace std;


but this seems not to be the case. When I do, I get a compile error

Quote:
hash.cpp(70) : error C2065: 'hash_map' : undeclared identifier


When I right click on my code where I define my hash_map and look at the definition, I see different definitions that use three parameters instead of two.

Reply With Quote
  #3  
Old March 18th, 2009, 02:05 PM
f'lar's Avatar
f'lar f'lar is offline
Senior WeyrLeader
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 4,130 f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level)f'lar User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 11 h 49 m 55 sec
Reputation Power: 977
Send a message via Google Talk to f'lar
I know this is old now, but I just ended up here via a google search, and so others might as well.

The key to fixing complete's problem is that Visual Studio 2003 and 2005 put hash_map in the stdext namespace rather than just std.

Take that to heart and everything is suddenly fine again.
Comments on this post
sizablegrin agrees: Same for VC++ 2008
__________________
Primary Forums: .Net Development, MS-SQL, C Programming
VB.Net: It's not your father's Visual Basic.

[Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Tip about STL hash_map and string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT