| prw8864 (6) | |
|
I have written up a class to facilitate sorting of maps and vectors via a sorting map. I have added questions in the forms of comments. Any help would be appreciated. #include <vector> #include <algorithm> #include <map> #include <string> #define dic_struc string, int using namespace std; template <class T, class U> class sorting_map { private: map<T, U> s_map; // can this be declaired as a pointer public: // such that the class would not have to hold the map continence?? sorting_map( map<T, U> s_ ) : s_map(s_) {}; // void operator[](map<T, U> *s_) : s_map(s_) {}; // this would allow the user to change sorting maps on the fly bool operator()(T x, T y) { return s_map[x] < s_map[y]; } // converts x to assigned value and compairs by values }; int main() { vector<int> v; typedef map<dic_struc> dic_map; dic_map tmap; dic_map::iterator ss_it; tmap["a"] = 5; // make my own sorting sequence tmap["b"] = 1; tmap["c"] = 3; tmap["e"] = 6; tmap["f"] = 10; tmap["g"] = 8; sorting_map<dic_struc> srt(tmap); //sort(v.begin(), v.end(), srt); // how can this be written current causes many errors map<string,int,sorting_map<string, int> > mymap3(tmap); map<dic_struc,sorting_map<dic_struc> > mymap4(tmap); return 0; } | |
|
|
|
| Cubbi (1584) | |||
This: #define dic_struc string, int is not a good idea.You are correct to question storing the entire map as a data member of sorting_map. I would use a const reference:
demo: http://ideone.com/5uqqC | |||
|
|
|||
| prw8864 (6) | |
| Thanks for the tips I will incorporate them into future development. | |
|
|
|