std :: map关联容器问题 [英] std::map associative container question

查看:105
本文介绍了std :: map关联容器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想知道是否有办法使用std :: map来存储

不同的类型之一它的两种类型。也就是说,我正在尝试使用

它作为:


typedef std :: map< string,doubleMap;


但不是双倍,我有整数,甚至是布尔值,所以我猜这个使用双重浪费了很多存储空间。有没有办法做到这一点?

也许有一个姓名?


谢谢大家,


a ^ 2

Hello everyone,

I would like to know if there is a way to use the std::map to store
different types for one of its two types. That is, I''m trying to use
it as:

typedef std::map<string,doubleMap;

but instead of double, I have integers, even booleans so I guess that
the use of double waste a lot of storage. Is there any way to do this?
Maybe with a typename?

Thank you all,

a^2

推荐答案

aaragon写道:

aaragon wrote:


I想知道是否有办法使用std :: map来存储

两种类型之一的不同类型。也就是说,我正在尝试使用

它作为:


typedef std :: map< string,doubleMap;


但不是双倍,我有整数,甚至是布尔值,所以我猜这个使用双重浪费了很多存储空间。有没有办法做到这一点?

也许有一个打字机?
I would like to know if there is a way to use the std::map to store
different types for one of its two types. That is, I''m trying to use
it as:

typedef std::map<string,doubleMap;

but instead of double, I have integers, even booleans so I guess that
the use of double waste a lot of storage. Is there any way to do this?
Maybe with a typename?



看一下boost :: any。如果这没有帮助,你可能需要更多地描述你想要做的事情。顺便说一下,虽然我可以看到没有使用双打的充分理由,但为什么你要关心浪费

存储?地图真的那么大吗?


祝你好运,


Tom

Take a look at boost::any. If that''s no help, you may need to describe
a bit more what it is you are trying to do. By the way, although I can
see good reasons not to use doubles, why do you care about wasting
storage? Is the map really that big?

Best regards,

Tom



Thomas Tutone写道:

Thomas Tutone wrote:

aaragon写道:

aaragon wrote:


我想知道是否有办法使用std :: map存储

两种类型之一的不同类型。也就是说,我正在尝试使用

它作为:


typedef std :: map< string,doubleMap;


但不是双倍,我有整数,甚至是布尔值,所以我猜这个使用双重浪费了很多存储空间。有没有办法做到这一点?

也许有一个打字机?
I would like to know if there is a way to use the std::map to store
different types for one of its two types. That is, I''m trying to use
it as:

typedef std::map<string,doubleMap;

but instead of double, I have integers, even booleans so I guess that
the use of double waste a lot of storage. Is there any way to do this?
Maybe with a typename?



看一下boost :: any。如果这没有帮助,你可能需要更多地描述你想要做的事情。顺便说一下,虽然我可以看到没有使用双打的充分理由,但为什么你要关心浪费

存储?地图真的那么大吗?


祝你好运,


Tom


Take a look at boost::any. If that''s no help, you may need to describe
a bit more what it is you are trying to do. By the way, although I can
see good reasons not to use doubles, why do you care about wasting
storage? Is the map really that big?

Best regards,

Tom


$ b $嗯,不是地图很大,而且当你请求密钥时,

然后返回值。因此,有时候我会期待一个bool

但是我得到了一个双倍(我不想转换

值也不想放一个前面的说明符为(bool)例如)。我是b / b
想知道可能有一种神圣化的方式来做到这一点。对于

示例,


模板< typename T>

....

typedef std :: map< string,TMap;


但我认为它不会起作用.....

Well, it''s not that the map is big, but also when you request the key,
then the value is returned. Therefore, sometimes I''m expecting a bool
but instead I''m receiving a double (and I don''t want to convert the
values nor put a specifier in front as (bool) for example). I was
wondering that maybe there is a templetized way to do this. For
example,

template<typename T>
....
typedef std::map<string,TMap;

but I don''t think it may work.....

aaragon写道:
aaragon wrote:

Thomas Tutone写道:
Thomas Tutone wrote:

aaragon写道:

aaragon wrote:


我想知道是否有办法使用std :: map存储

其中一个的不同类型类型。也就是说,我正在尝试使用

它:

>

typedef std :: map< string,doubleMap;

>

而不是双倍,我有整数,甚至是布尔值,所以我猜这个使用双倍浪费了大量的存储空间。有没有办法做到这一点?

也许有一个打字机?
I would like to know if there is a way to use the std::map to store
different types for one of its two types. That is, I''m trying to use
it as:
>
typedef std::map<string,doubleMap;
>
but instead of double, I have integers, even booleans so I guess that
the use of double waste a lot of storage. Is there any way to do this?
Maybe with a typename?



看一下boost :: any。如果这没有帮助,你可能需要更多地描述你想要做的事情。顺便说一下,虽然我可以看到没有使用双打的充分理由,但为什么你要关心浪费

存储?地图真的那么大吗?


祝你好运,


Tom

Take a look at boost::any. If that''s no help, you may need to describe
a bit more what it is you are trying to do. By the way, although I can
see good reasons not to use doubles, why do you care about wasting
storage? Is the map really that big?

Best regards,

Tom


$ b $嗯,不是地图很大,而且当你请求密钥时,

然后返回值。因此,有时候我会期待一个bool

但是我得到了一个双倍(我不想转换

值也不想放一个前面的说明符为(bool)例如)。我是b / b
想知道可能有一种神圣化的方式来做到这一点。对于

示例,


模板< typename T>

...

typedef std: :map< string,TMap;


但我认为它不会起作用.....


Well, it''s not that the map is big, but also when you request the key,
then the value is returned. Therefore, sometimes I''m expecting a bool
but instead I''m receiving a double (and I don''t want to convert the
values nor put a specifier in front as (bool) for example). I was
wondering that maybe there is a templetized way to do this. For
example,

template<typename T>
...
typedef std::map<string,TMap;

but I don''t think it may work.....



不,那绝对不行。 C ++是一种强类型语言,你需要使用特定类型来实例化地图。正如我所说,

看看boost :: any(参见 www.boost .ORG)。或者你也可以这样做:/ b $ b类似以下内容:


struct Aaragon_multi_type {

int i;

double d;

bool b;

};


std :: map< std :: string,Aaragon_multi_type地图;


空间效率不高,但会在

地图中存储任何值的商品......


再次,如果你不告诉我们你想做什么 - 也就是说,为什么你想要存储不同类型的
? - 提供

有用的建议非常困难。


祝你好运,


汤姆

No, that definitely won''t work. C++ is a strongly typed language, and
you need to instantiate the map using a particular type. As I said,
look at boost::any (see www.boost.org). Or maybe you can just do
something like the following:

struct Aaragon_multi_type {
int i;
double d;
bool b;
};

std::map<std::string, Aaragon_multi_type map;

Not space efficient, but would let store value of any of those in the
map...

Again, if you don''t tell us what it is you want to do - i.e., why do
you want to store different types? - it''s awfully hard to provide
useful advice.

Best regards,

Tom


这篇关于std :: map关联容器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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