简体   繁体   English

如何使用2个键值创建地图

[英]How to create a map with 2 key value

I am looking for something like a school time table. 我正在寻找学校时间表等内容。 Based on two input - Day of the week and Time of the day, one decides the subject. 根据两个输入-星期几和一天中的时间,决定主题。 To implement this in c++, I was thinking of something like "map < pair < int, int>, int>". 为了在c ++中实现这一点,我想到了“ map <pair <int,int>,int>”之类的东西。 I was reading here to use a key class and operator overloading. 我在这里阅读使用键类和运算符重载。 Is there any other elegant way of doing it? 还有其他优雅的方法吗?

Thanks in Advance 提前致谢

That's not a map with two keys (which would allow you to look up items from knowledge of just one key), it's a composite key, and map<pair<day, time>, subject> should work just fine. 那不是具有两个键的地图(这使您仅凭一个键就可以查找项目),而是一个复合键, map<pair<day, time>, subject>应该可以正常工作。

Also consider map<day, map<time, subject>> . 还考虑map<day, map<time, subject>>

You could typedef it. 您可以键入def。

typedef pair<int, int> key;
map<key, int> myMap = new map<key, int>();

You can't create a map with 2 key values, you can create a map with a complex key value (composed of two types). 您不能创建具有2个键值的地图,而可以创建具有复杂键值(由两种类型组成)的地图。 However, this won't let you look at the map for say just day of week regardless of time of day. 但是,不管一天中的什么时间,这都不能让您仅在一周的某一天查看地图。 I would create a new class to use as key, and some helper functions to be able to work by just day or just time. 我将创建一个新类用作键,并创建一些帮助器函数,使其仅需一天或一天​​即可工作。

Also, for your use case Boost.MultiIndex may help you. 另外,对于您的用例, Boost.MultiIndex可能会为您提供帮助。

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

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