C++ Data Structures – Struct vs std::pair

cdata structuresstl

I am a C++ programmer with limited experience.

Supposing I want to use an STL map to store and manipulate some data, I would like to know if there is any meaningful difference (also in performance) between those 2 data structure approaches:

Choice 1:
    map<int, pair<string, bool> >

Choice 2:
    struct Ente {
        string name;
        bool flag;
    }
    map<int, Ente>

Specifically, is there any overhead using a struct instead of a simple pair?

Best Answer

Choice 1 is ok for small "used only once" things. Essentially std::pair is still a struct. As stated by this comment choice 1 will lead to really ugly code somewhere down the rabbit hole like thing.second->first.second->second and no one really wants to decipher that.

Choice 2 is better for everything else, because it is easier to read what the meaning of the things in the map are. It is also more flexible if you want to change the data (for example when Ente suddenly needs another flag). Performance should not be an issue here.