Strange Behevior when trying to create a Template containing maps

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danalovesc
    New Member
    • Sep 2010
    • 9

    Strange Behevior when trying to create a Template containing maps

    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:

    Code:
    #ifndef __GARAGE_H_
    #define __GARAGE_H_
    #include <map>
    using namespace std;
    template <class T,class SortKey, class SearchKey>
    class GarageDataBase
    {
    public :
    
    	GarageDataBase();
    	virtual ~GarageDataBase();	
        const T& Top() const;
    	bool Add(T data,SortKey key2, SearchKey key2);
    	T Remove(SearchKey toRemove);
    	T Find(SearchKey toFind) const;
    	bool isEmpty()const;
    	
    
    
    private:
    	multimap<SortKey,T> firstMap;
    	multimap<SearchKey,pair<SortKey,T>*> secondMap;
    
    };
    #endif
    
    
    
    template <class T,class SortKey, class SearchKey> GarageDataBase<T,SortKey,SearchKey>::GarageDataBase()
    {
    
    }
    
    template <class T,class SortKey, class SearchKey> GarageDataBase<T,SortKey,SearchKey>::~GarageDataBase()
    {
    }
    
    template <class T,class SortKey, class SearchKey> const T& GarageDataBase<T,SortKey,SearchKey>::Top() const
    {
    	firstMap.
    }

    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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Let me try this again. Please ignore my previous post.

      Code:
      multimap<SortKey,int> firstMap; 
        pair<SortKey, int> var;
        SortKey key;
        multimap<SortKey, int>::iterator itr;
       itr = firstMap.find(key);
       itr->first;
        //OR
       (*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.

      Comment

      Working...