简体   繁体   中英

Which is the best data structure to store 1 key and 2 values in c++

I want to store data that looks like this:

NAME SURNAME BIRTHDATE

eg:

{{"Anna", "Smith", "16/06/1965"}
{"Bob", "Smith", "26/07/1982"}}

I think I can make a struct myself like this

struct PEOPLE {
    string name;
    string surname;
    string birthdate;
};

But is there any data structure I can use for this purpose? Like where a name is a key and surname and birthdates are values?

You can always have something like:

using Key   = std::string;
using Value = std::pair<std::string, std::string>;

and use it like:

using HumanBeing = std::pair<Key, Value>;

or:

std::map<Key, Value>

Of course, aliases are not that necessary:)

You could try using map or multimap , with name as key and a struct (with surname , birthdate , and whatever else you want in it) as value, but note that however you do it, in your application you will probably want the key to be unique for every entry, and name will possibly not be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM