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

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

问题描述

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



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



我正在工作表本质上有两个键。



例如,第一级键是枚举:

 枚举key_type {
E_ERROR = 0,
E_INT = 1,
E_CHAR = 2,
E_STR = 3,
} ; //是的,我知道你不必指定枚举的值

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

 键:E_INT 
二级键示例:1,2,3,4

键:E_CHAR
二进制键示例:'a','b','c','d'

键:E_STR
二进制键示例:abc,xyz,pdq ,jrr

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

  + -------- + 
| E_INT | ------------------------------> + --------------- --- +
+ -------- + |带INT键的MAP
| E_CHAR | --------------- \ + ------------------ +
+ ------ - + \
| E_STR | ------ \ \ ----> + ------------------- +
+ ------ - + \ | MAP与CHAR键|
\ + ------------------- +
\
\ ------> + - ---------------- +
| MAP与STR密钥|
+ ------------------ +

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



我完全坚持这种想法吗?如果它不坚果,你有没有任何建议如何继续这个?



我的头顶部我需要一个继承类的密钥,其中基类为sort方法提供了一个纯虚函数,然后继承了 E_INT E_CHAR 的键类 E_STR ,它们使用 sort()方法。



注释?






EDIT 8/13/2010



我一直在尝试一些解决方案,思想。我不断打击问题。我在另一篇stackoverflow文章中提到过类型擦除,这可能会为我的不同






EDIT 8/16/2010 b $ b

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

解决方案

std :: map 需要密钥的严格弱排序。如果您可以使用自定义比较器对不同的键类型强制执行单一订单,那么应该不会有问题。


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

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 |
                            +------------------+

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?

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.

Comments?


EDIT 8/13/2010

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 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天全站免登陆