By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Strange Behevior when trying to create a Template containing maps

P: 9
Hi all, i'm new to Templates and when i tried to implement templates methods which needs to use a map (which is a member in private), the map can only do 3 things: insert, swap, and operator =. and i don't get the full functionality of map..

Here's the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef __GARAGE_H_
  3. #define __GARAGE_H_
  4. #include <map>
  5. using namespace std;
  6. template <class T,class SortKey, class SearchKey>
  7. class GarageDataBase
  8. {
  9. public :
  10.  
  11.     GarageDataBase();
  12.     virtual ~GarageDataBase();    
  13.     const T& Top() const;
  14.     bool Add(T data,SortKey key2, SearchKey key2);
  15.     T Remove(SearchKey toRemove);
  16.     T Find(SearchKey toFind) const;
  17.     bool isEmpty()const;
  18.  
  19.  
  20.  
  21. private:
  22.     multimap<SortKey,T> firstMap;
  23.     multimap<SearchKey,pair<SortKey,T>*> secondMap;
  24.  
  25. };
  26. #endif
  27.  
  28.  
  29.  
  30. template <class T,class SortKey, class SearchKey> GarageDataBase<T,SortKey,SearchKey>::GarageDataBase()
  31. {
  32.  
  33. }
  34.  
  35. template <class T,class SortKey, class SearchKey> GarageDataBase<T,SortKey,SearchKey>::~GarageDataBase()
  36. {
  37. }
  38.  
  39. template <class T,class SortKey, class SearchKey> const T& GarageDataBase<T,SortKey,SearchKey>::Top() const
  40. {
  41.     firstMap.
  42. }
  43.  

in the last func when trying to get into firstMap mathods, all i get is: insert, swap or, =...


how do i get to "first" or s"econd" in the map?


Thank you and sorry i'm just a beginner here
Sep 18 '10 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
firstMap.first and firstMap.second.

Remember, map<> entries are pair<> objects.

first and second are public data members of the pair<> used in the map<> entry.
Sep 18 '10 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Let me try this again. Please ignore my previous post.

Expand|Select|Wrap|Line Numbers
  1. multimap<SortKey,int> firstMap; 
  2.   pair<SortKey, int> var;
  3.   SortKey key;
  4.   multimap<SortKey, int>::iterator itr;
  5.  itr = firstMap.find(key);
  6.  itr->first;
  7.   //OR
  8.  (*itr).first;
maps are trees with pair entries. The pair has two public members first and second.

So in the code above, you have a map. Then there is a pair<SortKey, int> object named var. var can be inserted in the map.

To find a pair object inside a map you can use the map's find method. This method returns an iterator to the pair that was located in the map. An iterator is a pointer to that pair.

So an iterator itr was created.

Then you find the pair<> using the map's find method. It returns an iterator to a pair<SortKey, int>. Using that iterator, you access the pair's first and second members.

If the iterator is equal to firstMap.end(), then yur key was not located in the map.
Sep 18 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.