LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2007, 04:55 AM   #1
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
C++ STL hash ???


I want a simple hash in C++
i was trying the STL and on SGI web site is this:
Code:
int main()
{
  hash<const char*> H;
  cout << "foo -> " << H("foo") << endl;
  cout << "bar -> " << H("bar") << endl;
}
which will not compile
Code:
g++    -c -o 1.o 1.cc
1.cc: In function `int main()':
1.cc:8: error: `hash' was not declared in this scope
1.cc:8: error: expected primary-expression before "const"
1.cc:8: error: expected `;' before "const"
make
great example eh?
I added the std namespace




any clues as to what to #include?

I've tried <hash> <hash.h> hash_map

searched here and on koders.org etc...


also tried
Code:
#include <ext/hash_map>
#include <ext/hash_set>
#include <ext/hash_fun.h>

Last edited by bigearsbilly; 07-04-2007 at 05:10 AM.
 
Old 07-04-2007, 05:19 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
doh!

Quote:
This class is an SGI extension; it is not part of the C++ standard.
always read the whole page
 
Old 07-04-2007, 05:46 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
There is the standard map but the last time I used c++ there was no standard hash (the sorting can be a problem)
SGI does an extension to this:
http://www.sgi.com/tech/stl/hash_map.html
That's not so clear on their website, I remember it now

Code:
#include <ext/hash_map>
#include <iostream>
#include <string.h>

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

int main()
{
 using namespace std;
 using namespace __gnu_cxx;

  hash_map<const char*, int, hash<const char*>, eqstr> months;
  
  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;
  
  cout << "september -> " << months["september"] << endl;
  cout << "april     -> " << months["april"] << endl;
  cout << "june      -> " << months["june"] << endl;
  cout << "november  -> " << months["november"] << endl;
}
 
Old 07-04-2007, 10:16 AM   #4
atulsvasu
Member
 
Registered: Apr 2006
Distribution: Gentoo
Posts: 49

Rep: Reputation: 15
In g++, extensions are available in
__gnu_cxx namespace not in std.
 
Old 07-05-2007, 02:21 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
thanks,
yes I ended up using a map
 
Old 07-05-2007, 05:26 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
The STL is really good but some functions add a very high complexity (O(n)..) to your program. Iterators or find algo are such ones, it can happen that they recreate a complete class on every iteration.
In case you don't know these libraries, it's worth mentionning: http://www.boost.org
These are used in big environments that have strong requirements in term of speed.
 
Old 07-05-2007, 05:43 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
cheers,

complexity or re-invent the wheel?
I am lazy first and foremost.

It's a test for a job-interview, an arithmetical expression parser.
So I want to show I know how to use STL, multiple inheritance, exceptions, polymorph etc.

I ain't done C++ since college (and I can see why)

God it's a hideous language.


as ugly as perl and half as much fun.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello from STL oakleyd LinuxQuestions.org Member Intro 1 12-15-2006 11:03 PM
STL 2D Array lramos85 Programming 5 07-12-2006 05:34 PM
what will be the return value? ( STL and c++) ankit4u1 Programming 3 06-15-2006 01:59 PM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM
c++ and stl G67 Programming 4 12-17-2003 02:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration