Let's assume there are 2 classes: Country and City. Both of them have names, so a map would suit as conainer
map<std::string, Country> countries;
In the country class there is a similar map that contains all cities - for the sake of simplicity, let's say it's public:
map<std::string, City> cities;
Now on top level if a specific city should be addressed, the request looks like this:
(In the real world I should rather use
find and check every time if the desired country/city does actually exist, right?)
Now let's say at top level we want to store in a vector a set of our favourite cities. I see 2 options to do so:
// Option 1: Store the cities as references: vector<City&> favouriteCities; // Option 2: Store the ID's (Names) of the countries and the cities as strings vector<pair<std::string,std::string>> favouriteCities;
So to get my first favourite city in option 2 I'll have to access it like this:
What would be the pros and cons of these options? AFAIK: Option 1 is more performant and more convenient. But with option 2 you have te chance to check if the desired favourite city is still in the system and has not been deleted in the meantime (so a reference would be invalid).
Are there more points to consider?