简体   繁体   English

哪个是在 c++ 中存储 1 个键和 2 个值的最佳数据结构

[英]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.您可以尝试使用mapmultimap ,以name作为键和struct (带有surnamebirthdate以及您想要的任何其他内容)作为值,但请注意,无论您怎么做,在您的应用程序中您可能希望密钥每个条目都是唯一的, name可能不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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