Let's assume there are 2 classes: Country and City. Both of them have names, so a map would suit as conainermap<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:countries.at(Japan).cities.at(Tokyo);(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 stringsvector<pair<std::string,std::string>> favouriteCities;So to get my first favourite city in option 2 I'll have to access it like this:countries.at(favouriteCities[0].first).cities.at(favouriteCities[0].second);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?
C++ object references vs. lookup
c++;data structures;stl