-
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
-
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;
}
-
Thanks,
Exactly what does the pair function do map....Map the object to
the key?
-
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?
-
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.
-
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.
-
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.