Sign in to follow this  
supagu

[linux] hash_multimap

Recommended Posts

i have a hash_multimap from some code i use under win32. Im porting it to linux and its something like this:
#include <hash_map>

struct OccluderNode
{
};

class Occluder
{
public:

  typedef hash_multimap<void*, OccluderNode>    OccluderHash;
};

the error i get from this is: error: ISO C++ forbids declaration of 'hash_multimap' with no type i've tried using the includes: #include <hash_map> and: #include <hash_multimap> neither fix the problem, some other include? or some other include that doesnt come standard with linux install?

Share this post


Link to post
Share on other sites
hash_map and hash_multimap aren't standard AFAIK. However, on my Linux box, I get these results:
$ locate hash_map
/usr/lib64/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/ext/hash_map
/usr/lib64/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/debug/hash_map
/usr/lib64/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/debug/hash_map.h
/usr/lib64/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/backward/hash_map.h
/usr/include/asio/detail/hash_map.hpp
/usr/include/boost/serialization/hash_map.hpp
And this code seems to compile:
#include <ext/hash_map>

int main() {
__gnu_cxx::hash_multimap<void*, int> foo_map;
}
I'm using gcc version 4.1.2 (Gentoo 4.1.2)

It does look highly unportable though. Perhaps you should look into some 3rd-party portable library that offers hash maps.

Share this post


Link to post
Share on other sites
With GCC, it's:
#include <ext/hash_map>

And it's in the __gnu_cxx namespace. Going forward, you may want to try the TR1 "unordered_map" which has a standard interface that will be implemented the same across implementations. I've heard of problems with different "hash_map" implementations.

Share this post


Link to post
Share on other sites
ah yeah, its not working under linux.
also unordered_map doesnt seem to be a valid option yet.

probably have to implement some thing my self, miltimap of lists :-/

Share this post


Link to post
Share on other sites
Quote:
Original post by supagu
ah yeah, its not working under linux.
also unordered_map doesnt seem to be a valid option yet.


Which version of GCC are you using? It's definitely present and working with g++ 4.1.


#include <tr1/unordered_map>
#include <string>
#include <iostream>
#include <sstream>

typedef std::tr1::unordered_multimap<std::string, int> str_multimap;

int main() {
str_multimap strings;
std::stringstream sstream;
std::string key;

for(int i = 0; i < 5; ++i) {
sstream.str("");
sstream << i;
key = "key " + sstream.str();
strings.insert(std::make_pair(key, i));
strings.insert(std::make_pair(key, i * 2));
}

for(str_multimap::const_iterator it = strings.begin();
it != strings.end(); ++it) {
std::cout << it->first << ": " << it->second << "\n";
}

}





$ g++ --version
g++ (GCC) 4.1.3 20070528 (prerelease) (Debian 4.1.2-10)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -o foo foo.cc -Wall -Werror
$ ./foo
key 0: 0
key 0: 0
key 2: 2
key 2: 4
key 4: 4
key 4: 8
key 1: 1
key 1: 2
key 3: 3
key 3: 6



Quote:
probably have to implement some thing my self, miltimap of lists :-/


You should probably consider a 3rd party implementation of the SGI extensions to C++ like STLport before rolling your own. Or use the SGI extensions provided by GCC as abdulla and Oxyd have show you. I'd go with the TR1 containers when possible though.

Hope this helps.

EDIT: fixed output


[Edited by - let_bound on June 3, 2007 11:01:12 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this