STL Map Object

This is a discussion on STL Map Object within the C++ Programming forums, part of the General Programming Boards category; Hi, Am confused how STL Map object works... Say I want to store an employee with various attributes id name ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    24

    STL Map Object

    Hi,

    Am confused how STL Map object works...
    Say I want to store an employee with various attributes
    id name pay etc in a map. I can use the ID as a key but am unsure as to the actual setup. Do I include a map object in my employee class for the id and trace from there as I dont believe I can store an entire employee object in the map.

    can someone please clarify this

    Regards
    Nomes

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I think the normal way the STL map is used is to store an entire employee in the map. You have a class (e.g. Employee) with all the data (e.g. name, pay, etc.). The class can also have the ID as one of its members if you want. Then you have the map outside of the class wherever you need a map of Employees. Use the ID as the key to the map and Employee (or Employee* or a smart pointer -- not auto_ptr -- that holds an Employee*) as the value in the map.
    Code:
    #include <map>
    
    class Employee
    {
    public:
        Employee(int newID) : m_ID(newID) { }
        ~Employee() { }
        // and copy constructor and operator= if not simple data
    
        int GetID() const { return m_ID; }
        // ... all the interface methods
    
    private:
        int m_ID;
        // ... all the rest of the data
    };
    
    int main()
    {
        // Create some Employees with different IDs.
        int nextID = 1;
        Employee emp1(nextID++);
        Employee emp2(nextID++);
    
        std::map<int, Employee> myEmployeeMap1;
        myEmployeeMap1.insert(std::make_pair(emp1.GetID(), emp1));
        myEmployeeMap1.insert(std::make_pair(emp2.GetID(), emp1));
    
        // or
    
        Employee* pEmp3 = new Employee(nextID++);
        Employee* pEmp4 = new Employee(nextID++);
    
        std::map<int, Employee*> myEmployeeMap2;
        myEmployeeMap2.insert(std::make_pair(pEmp3->GetID(), pEmp3));
        myEmployeeMap2.insert(std::make_pair(pEmp4->GetID(), pEmp4));
    
        // ... use the maps
    
        delete pEmp3;
        delete pEmp4;
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    24
    Thanks,

    Exactly what does the pair function do map....Map the object to
    the key?

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    24
    Hey,

    Tried the above to insert Team into Map
    I am tokenising a file once each line has been
    tokenised I instantiate a team object pTeam1 using the tokens
    then add the team to the map - Only the first one is being inserted, as when I use the size function it is always one. However the make_pair getCode is executing...

    Code:
    std::map<string, Team*> mapTeam;
    Team* pTeam1 = new Team(countryName,code);
    mapTeam.insert(std::make_pair(pTeam1->getCode(), pTeam1));
    size=mapTeam.size();
    any ideas?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    What the make_pair does is to combine the key and the value together into a single object that gets inserted into the map.

    The point of a map is to hold some value, and have that value be accessible by some key. So, in that example, you are storing Teams. The Team is the value, or more specifically, a pointer to a Team object. The map allows you fast access to find a Team based on some key, in this case the Code. If you know the Code you can look up the Team in the map.

    All make_pair does is create an object of type std::pair that combines the key and the value into a single "thing". The STL map holds those "things". Each time you call insert, you insert one key-value pair. That is why your size is 1, you only inserted 1 thing into the map.

    Here is more code that shows you how to access objects in the map:
    Code:
    std::map<string, Team*> mapTeam;
    Team* pTeam1 = new Team(countryName,code);
    int code1 = pTeam1->getCode();  // Save the code for later
    mapTeam.insert(std::make_pair(pTeam1->getCode(), pTeam1));
    // ... insert more teams
    size=mapTeam.size();
    // ... do things
    
    // Search for the team with the saved code above.
    std::map<string, Team*>::iterator iterTeam = mapTeam.find(code1);
    
    // If the returned iterator == the end of the map, that key was not found.
    if (iterTeam != mapTeam.end())
    {
        // The map just holds std::pair<string, Team*> "things".
        // So get the one we just found with the saved Code.
        std::pair<string, Team*> team1pair = *iterTeam;
    
        // Use the team.
        Team* pFoundTeam = team1pair.second;
        pFoundTeam->DoWhatever();
    }
    Hopefully that gives you a better idea of what the map holds and why the make_pair is used. I'd suggest searching for a good book and/or tutorial on these that can explain it better and in more detail than I can.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    24
    I understand the make_pair will insert one object into the map but my program reads in a file of team info I tokenise each line to make a team object, I then insert that pteam1 object into the map ie each time a new line is read pTeam1 is a new object with new attribute values...so the 1st pTeam1 code will be different from the next line read....but is only inserting one...Essentially the above code is in a loop until the EOF is reached.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You must have different keys for each insert. The map method only holds one value per key. If the pTeam1->getCode() returns the same value every time, then you will only insert one object and the rest will be discarded.

    The only reason you really would want to use a map is if you have a unique key for each team. Otherwise, you would use a vector or list or deque, etc. If pTeam1->getCode() is supposed to be unique each time through the loop, then there is some problem with the rest of your program.

    In some cases you would use a multimap if the key is usually unique but not always, but I don't think that is necessarily what you are looking for here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a map with a new object type
    By blacknail in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 10:16 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. n00b question regarding the Map Class in the STL library
    By Axegrinder#9 in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 09:40 PM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21