0
class Garage { string WorkerFirstName; string WorkerLastName; string WorkerNumber; public: Garage() { WorkerFirstName = ""; WorkerLastName = ""; WorkerNumber = ""; } void SetFirstName(string FirstName) { WorkerFirstName = FirstName;} void SetLastName(string LastName) { WorkerLastName = LastName; } void SetNumber(string Number) { WorkerNumber = Number; } string GetFirstName() { return WorkerFirstName; } string GetLastName() { return WorkerLastName; } string GetNumber() { return WorkerNumber; } }; class GarageList { Garage List[500]; int MaxSize; int Size; public: GarageList() { MaxSize = 500; } ... //list out functions }; 

That is an abridged version of my setup. I can't figure out how to make a map with a reference id based on last name and a value which would contain all of the attributes of the garage class. I guess something like map< string, Garage List[500] > directory.

2 Answers 2

2

You are close. What you are looking for is:

map<string, Garage>; 

With this in your GarageList class, you don't need to worry about number of entries, the map will manage that for you.

So GarageList would look like this:

class GarageList { map<string, Garage> garages_; public: GarageList() { } int Size() { return garages_.size(); } void AddGarage(const Garage& garage) { garages_[garage.GetLastName()] = garage; } // Return true if found, false otherwise bool FindGarage(const string& lastName, Garage& foundGarage) { if (garages_.find(lastName) != garages_.end()) { foundGarage = garages_[lastName]; return true; } return false; // garage not found with lastName } return ... //list out functions }; 
Sign up to request clarification or add additional context in comments.

2 Comments

Well I got it to return 0, but when I create a new record into the array it doesn't reflect so in the map. And how would I make the key of the map based on the WorkerLastName ?
t's not clear to me how important this list of Garage(s) is to you so let me write a little more of this class by editing my answer (adding garage and finding garage).
1

You mean you want a map of name to an array ? Then you can use map<string, vector<Garage> >.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.