bonjour,
voici plusieurs jours que je lute pour trouver une solution a mon problème. Je souhaiterai créer une classe map personnalisé qui ajouterait une fonction permettant de trouver une clef par la valeur (donc la fonction inverse de find).
J'aimerais utiliser cette technique parce que d'une part je trouve que c'est plus propre que d'utilisé 2 map complémentaires et d'autre part d'un point de vue éducatif je souhaiterais comprendre pourquoi mon code ne fonctionne pas...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 #ifndef _MAPINTOWWAYS_H #define _MAPINTOWWAYS_H #include <map> #include <iostream> #include <algorithm> using namespace std; /* * fonction objet permetant de comparer une valeur avec celui d'un élement d'une map */ template <class K, class V> class value_equals { private: V value; public: // constructeur (initialise la valeur à comparer) value_equals (const V& v) : value(v) { } // comparaison bool operator() (pair<const K, V> elem) { return elem.second == value; } }; /* * classe template dérivant de la classe template map */ template <typename _Key, typename _Tp, typename _Compare = less<_Key>, typename _Alloc = allocator<pair<const _Key, _Tp> > > class mapinTowWays : public map <_Key, _Tp, _Compare, _Alloc > { public: mapinTowWays(){} mapinTowWays(const mapinTowWays& orig){} virtual ~mapinTowWays(){} //ma fonction permettant de récupérer la clef en fonction de la valeur (si la valeur est unique) _Alloc findByValue(_Tp value ){ return find_if(this->begin(),this->end(),value_equals<_Key,_Tp>(value)); } //idem avec un 2eme essai qui ne marche pas //ma fonction permettant de récupérer la clef en fonction de la valeur (si la valeur est unique) /* map<_Key,_Tp>::iterator findByValueV2(_Tp value ){ map<_Key,_Tp>::iterator iter; iter=find_if(this->begin(),this->end(),value_equals<_Key,_Tp>(value)); return iter; } */ }; #endif /* _MAPINTOWWAYS_H */voila, je poste également les erreurs de compile:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include "mapinTwoWays.h" #include <map> #include <string> using namespace std; int main(int argc, char** argv) { //ca ca marche map<string,int> mymap; mymap["azerty"]=2; map<string,int>::iterator iter = find_if(mymap.begin(),mymap.end(),value_equals <string,int>(2)); cout << "key: " << iter->first << "\tvalue: " << iter->second <<endl; //ca ca compile pas mapinTwoWays<string,int> mysupermap; mysupermap=["azerty"]=2 mapinTwoWays<string,int>::iterator iter = mysupermap.findByValue(2); cout << "key: " << iter->first << "\tvalue: " << iter->second <<endl; return (EXIT_SUCCESS); }
--> erreur avec la fonction commentée /*findByValueV2(_Tp value )*/
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.cpp
..\main.cpp: In function `int main(int, char**)':
..\main.cpp:16: error: `mapinTwoWays' was not declared in this scope
..\main.cpp:16: error: expected primary-expression before ',' token
..\main.cpp:16: error: expected primary-expression before "int"
..\main.cpp:16: error: expected `;' before "int"
..\main.cpp:17: error: `mysupermap' was not declared in this scope
..\main.cpp:17: error: expected primary-expression before '[' token
..\main.cpp:18: error: expected `;' before "mapinTwoWays"
..\main.cpp:16: warning: unused variable 'mapinTwoWays'
..\main.cpp:17: warning: unused variable 'mysupermap'
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algo.h: In function `_InputIterator std::find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::string, int> >, _Predicate = int]':
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algo.h:336: instantiated from `_InputIterator std::find_if(_InputIterator, _InputIterator, _Predicate) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::string, int> >, _Predicate = int]'
..\main.cpp:12: instantiated from here
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algo.h:187: error: `__pred' cannot be used as a function
Build error occurred, build is stopped
Time consumed: 390 ms.
voila. si quelqu'un a une idée, ca serai vraiment trés sympa de me la faire partager parce que moi j'en ai plus...
(PS: j'ai bien regardé le FAQ sur les templates concernant l'héritage avant de poster mais je n'y ai pas trouvé ma réponse)
cordialement
Partager