可以使用不同大小的键的STL地图 [英] Can a STL map be used with keys of varying sizes

查看:105
本文介绍了可以使用不同大小的键的STL地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用不同大小的键的STL地图吗?

Can a STL map be used for keys of varying sizes?

我没有代码。我还在试图弄清楚这是否可以完成,因此我的问题。 (我是可以花费太多时间在不可能的问题上的类型,我希望从你的智慧中学习)。

I don't have code for this. I'm still trying to figure out if this can be done and hence my question. (I'm the type that can spend too much time on an impossible problem. I'm hoping to learn from your wisdom).

我正在查找表基本上有两个键。一个数字型键和一个特定类型的次级键。

I am working on a look up table that essentially has two keys. A numeric type key and a type specific secondary key.

例如,第一级键是枚举:

For example the first level key is an enumeration:

enum key_type {
  E_ERROR = 0,
  E_INT   = 1,
  E_CHAR  = 2,
  E_STR   = 3,
}; // Yes I know you don't HAVE to specify the values for the enumeration

然后次要键取决于key_type。 E_INT 的辅助键是一个整数, E_CHAR 是一个字符等。

And then the secondary keys depend upon the key_type. The secondary key for an E_INT is an integer, for an E_CHAR is a character, etc.

Key: E_INT
2ndary Key Examples: 1, 2, 3, 4

Key: E_CHAR
2ndary Key Examples: 'a', 'b', 'c', 'd'

Key: E_STR
2ndary Key Examples: "abc", "xyz", "pdq", "jrr"

我的第一反应是使这个地图指针数组。第一级键用作数组的索引。数组索引指向支持辅助键类型的地图。

My first reaction was to make this an array of map pointers. The first level key is used as the index into the array. Where the array index points at a map that supports the secondary key type.

+--------+
| E_INT  |------------------------------>+------------------+
+--------+                               | MAP with INT key |
| E_CHAR |---------------\               +------------------+
+--------+                \
| E_STR  |------\          \---->+-------------------+
+--------+       \               | MAP with CHAR key |
                  \              +-------------------+
                   \
                    \------>+------------------+
                            | MAP with STR key |
                            +------------------+

知道我可以得到以上的工作,但我想我可以组合两个键,并有一个单一的地图,与一个自定义的 sort()算法来处理组合键。

I know I can get the above to work, but I was thinking I could combine the two keys and have a single map, with a custom sort() algorithm to deal with the combined keys.

我是否完全想到这一点?如果它不坚果,你有什么建议如何进行这个?

Am I completely nuts for thinking of this? If its not nuts, do you have any suggestions on how to proceed with this?

在我的头顶我需要为一个继承类的钥匙,基类为sort方法提供了一个纯虚拟函数,然后为 E_INT E_CHAR E_STR ,它们实现了 sort()方法。然后我会使用基础键作为地图的键。

Off the top of my head I need to make an inherited class for the key where the base class provides a pure virtual function for the sort method, and then have inherited key classes for the E_INT, E_CHAR and E_STR, that implement the sort() method for their usage. Then I would use the base key class as the key for the map.

评论?

编辑8/13/2010

我一直在尝试一些解决方案,以及我的原始想法。我不断遇到问题。我确实绊倒了另一个stackoverflow文章,提到类型擦除,这可能会为我的伎俩不同的钥匙。

I've been trying some solutions posed, as well as my original thoughts. I keep hitting problems. I did stumble across another stackoverflow article that mentioned type erasure which might do the trick for my differing keys.

EDIT 8/16/2010

在下面的答案部分添加了一个答案,显示了我实现的编码解决方案。

Added an answer in the answer section below that shows the coded solution I implemented.

推荐答案

std :: map 需要关键字的严格弱点订购。如果您可以使用自定义比较器对您的不同键类型执行单一顺序,则不应该是一个问题。

std::map requires strict weak ordering for the keys. If you can enforce single order on your different key types with custom comparator then it shouldn't be a problem.

这篇关于可以使用不同大小的键的STL地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆