C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[STL] utiliser une hash_map

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
C.B.
Guest





PostPosted: Sun Feb 26, 2006 2:06 pm    Post subject: [STL] utiliser une hash_map Reply with quote



Bonjour,
je souhaite utiliser une hah_map dans un projet. J'ai voulu faire un petit
fichier de test :

-----------

#include <string>
#include <ext/hash_map>

using namespace std;

struct eqint
{
bool operator()(int i1, int i2) const
{
return i1 == i2;
}
};

int main (int argc, char** argv)
{
hash_map<int, string> test;
hash_map<int, string, hash(int)> test1;
hash_map<int, string, hash(int), eqint> test2;

return 0;
}

--------

et a la compilation :
g++ testHashMap.cpp
testHashMap.cpp: In function 'int main(int, char**)':
testHashMap.cpp:16: error: 'hash_map' was not declared in this scope
testHashMap.cpp:16: error: expected primary-expression before 'int'
testHashMap.cpp:16: error: expected `;' before 'int'
testHashMap.cpp:17: error: expected primary-expression before 'int'
testHashMap.cpp:17: error: expected `;' before 'int'
testHashMap.cpp:18: error: expected primary-expression before 'int'
testHashMap.cpp:18: error: expected `;' before 'int'

(g++ --version
g++ (GCC) 4.0.1 (4.0.1-5mdk for Mandriva Linux release 2006.0))

J'imagine que ce doit être trivial, mais je ne vois pas ou est l'erreur ...
Back to top
C.B.
Guest





PostPosted: Sun Feb 26, 2006 5:06 pm    Post subject: Re: [STL] utiliser une hash_map Reply with quote



Mais quand j'utilise <hash_map>, il me dit fichier introuvable ...

Franck Branjonneau wrote:

Quote:
"C.B." <wang_zangkun (AT) hotmail (DOT) com> écrivait:

je souhaite utiliser une hah_map dans un projet.

#include <ext/hash_map

Ce n'est pas un entête standard, mais une extension g++. hash_map est
dans le namespace __gnu_cxx.
Back to top
Franck Branjonneau
Guest





PostPosted: Sun Feb 26, 2006 5:06 pm    Post subject: Re: [STL] utiliser une hash_map Reply with quote



"C.B." <wang_zangkun (AT) hotmail (DOT) com> écrivait:

Quote:
je souhaite utiliser une hah_map dans un projet.

#include <ext/hash_map

Ce n'est pas un entête standard, mais une extension g++. hash_map est
dans le namespace __gnu_cxx.

--
Franck Branjonneau
Back to top
C.B.
Guest





PostPosted: Sun Feb 26, 2006 7:06 pm    Post subject: autre pb de hash_map Reply with quote

J'ai modifié mon source pour que ça marche. Mais, hash_map<const char*, int,
hash<const char*>, eqstr> months marche bien (exemple de sgi), alors que
hash_map<const string, int, hash<const string>, eqstri> test me fait une
liste d'erreurs bien imbuvable.

------------

#include <string>
#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

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

struct eqstri
{
bool operator()(const string s1, const string s2) const
{
return s1 == s2;
}
};

int main (int argc, char** argv)
{
hash_map<const string, int, hash<const string>, eqstri> test;
test["oui"] = 0;

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;
}

-----------

g++ testHashMap.cpp
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ext/hashtable.h:
In member function 'size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>::_M_bkt_num_key(const _Key&, size_t) const
[with _Val = std::pair<const std::string, int>, _Key = const std::string,
_HashFcn = __gnu_cxx::hash<const std::string>, _ExtractKey =
std::_Select1st<std::pair<const std::string, int> >, _EqualKey = eqstri,
_Alloc = std::allocator<int>]':
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ext/hashtable.h:600:
instantiated from 'size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>::_M_bkt_num(const _Val&, size_t) const
[with _Val = std::pair<const std::string, int>, _Key = const std::string,
_HashFcn = __gnu_cxx::hash<const std::string>, _ExtractKey =
std::_Select1st<std::pair<const std::string, int> >, _EqualKey = eqstri,
_Alloc = std::allocator<int>]'
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ext/hashtable.h:1007:
instantiated from 'void __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>::resize(size_t) [with _Val =
std::pair<const std::string, int>, _Key = const std::string, _HashFcn =
__gnu_cxx::hash<const std::string>, _ExtractKey =
std::_Select1st<std::pair<const std::string, int> >, _EqualKey = eqstri,
_Alloc = std::allocator<int>]'
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ext/hashtable.h:795:
instantiated from 'typename __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>::reference __gnu_cxx::hashtable<_Val, _Key,
_HashFcn, _ExtractKey, _EqualKey, _Alloc>::find_or_insert(const _Val&)
[with _Val = std::pair<const std::string, int>, _Key = const std::string,
_HashFcn = __gnu_cxx::hash<const std::string>, _ExtractKey =
std::_Select1st<std::pair<const std::string, int> >, _EqualKey = eqstri,
_Alloc = std::allocator<int>]'
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ext/hash_map:233:
instantiated from '_Tp& __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
_Alloc>::operator[](const typename __gnu_cxx::hashtable<std::pair<const
_Key, _Tp>, _Key, _HashFcn, std::_Select1st<std::pair<const _Key, _Tp> >,
_EqualKey, _Alloc>::key_type&) [with _Key = const std::string, _Tp = int,
_HashFcn = __gnu_cxx::hash<const std::string>, _EqualKey = eqstri, _Alloc =
std::allocator<int>]'
testHashMap.cpp:26: instantiated from here
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ext/hashtable.h:596:
error: no match for call to '(const __gnu_cxx::hash<const std::string>)
(const std::basic_string<char, std::char_traits<char>, std::allocator<char>
Quote:
&)'
--------------


merci g++, c'est tout de suite plus clair. Qqn aurait une idée ?
Back to top
Franck Branjonneau
Guest





PostPosted: Sun Feb 26, 2006 11:06 pm    Post subject: Re: autre pb de hash_map Reply with quote

[ Merci de respecter les conventions en usage ]

"C.B." <wang_zangkun (AT) hotmail (DOT) com> écrivait:

Quote:
J'ai modifié mon source pour que ça marche. Mais, hash_map<const char*, int,
hash<const char*>, eqstr> months marche bien (exemple de sgi),

Tu utilises une extension de g++ -- pourquoi tu lis documentation de
sgi et pas celle de g++ ?

Quote:
alors que hash_map<const string, int, hash<const string>, eqstri
test me fait une liste d'erreurs bien imbuvable.

hash_map<const string, int, hash<const string>, eqstri> test;

Probablement

hash_map<const string, int, hash< string >, eqstri> test;

je n'ai pas lu la documentation.

Quote:
merci g++, c'est tout de suite plus clair. Qqn aurait une idée ?

Utiliser tr1::unordered_map ?

--
Franck Branjonneau
Back to top
Ploc
Guest





PostPosted: Mon Feb 27, 2006 2:06 pm    Post subject: Re: [STL] utiliser une hash_map Reply with quote

C.B. wrote:
Quote:
Mais quand j'utilise <hash_map>, il me dit fichier introuvable ...

il faut <ext/hash_map>
_et_ utiliser le namespace __gnu_cxx.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.