包含字符串键和模板化值的map ... [英] map containing a string key and a templated value...

查看:79
本文介绍了包含字符串键和模板化值的map ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在尝试创建一个地图/字典,其中键的类型已知

即std :: string,但值可以是任何内置类型。即。 int,

double等(地图上的内容<字符串,Twhere T是

模板参数)


To保持值类型我有类似的东西:


模板< class T>

struct数据

{

T值;

}


现在我正在尝试声明如下地图:


std :: map< std :: string,Data> ;;


这不会/不会工作,我不知道怎么声明它?


如果我有一个方法必须将此地图作为参数传递,我将如何使用


例如Foo(std :: map< string,Dataparam)不起作用,我不希望

它要么


有人可以请帮助!

Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)

To hold the value type I have something like:

template<class T>
struct Data
{
T value;
}

Now I am trying to declare a map as below:

std::map<std::string, Data>;

this doesn''t/wouldn''t work and I don''t know how to declare it??

if I have a method which has to pass in this map as a parameter, how
would I do it??
e.g Foo(std::map<string, Dataparam ) doesn''t work and I don''t expect
it to either

Can someone please help!

推荐答案

de * ****** @ hotmail.com schrieb:
de*******@hotmail.com schrieb:

您好,


我正在尝试创建一个地图/字典,其中键的类型是已知的

即std :: string,但该值可以是任何内置类型。即。 int,

double等(地图上的内容<字符串,Twhere T是

模板参数)


To保持值类型我有类似的东西:


模板< class T>

struct数据

{

T值;

}


现在我正在尝试声明如下地图:


std :: map< std :: string,Data> ;;


这不行/不行,我不知道怎么声明?
Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)

To hold the value type I have something like:

template<class T>
struct Data
{
T value;
}

Now I am trying to declare a map as below:

std::map<std::string, Data>;

this doesn''t/wouldn''t work and I don''t know how to declare it??



如果你想要一个异构容器,你需要运行时多态性;

你不能单独使用模板。


也就是说,使用一个虚拟基类和模板化的子类,它们实际类型为




最简单的方法是使用boost :: any或

boost :: variant<>,它们都做类似的事情:


std: :map< std :: string,boost :: anymapOfAnyType;


-

Thomas

If you want a heterogenous container, you need run-time polymorphism;
you can''t do it with templates alone.

That is, use a virtual base class and templated sub-classes which hold
the actual type.

The easiest way would be to use either of boost::any or
boost::variant<>, they both do something similar:

std::map<std::string, boost::anymapOfAnyType;

--
Thomas


非常感谢您的及时回复!我可以请问以下


1)会提升::任何让我存储任何类型,包括内置类型和

他们的网站上有很好的例子关于这个特殊的

功能?

2)当你说即使用虚拟基类并模板化

子保持实际类型的类。 - 你的意思是宣布

类似map< string,base *>?是否必须宣布基地

作为模板?

Many Thanks for your prompt reply! Can I please ask following

1) will boost::any let me store any type including built-in types and
are there good examples on their site with regards to this particular
feature?
2) when you said "That is, use a virtual base class and templated
sub-classes which hold the actual type." - did you mean declaring a
something like map<string, base*>? would the base have to be declared
as a template too?



de ******* @ hotmail.com 写道:

你好,


我正在尝试创建一个地图/字典,其中键的类型已知

即std :: string,但值可能是任何内置的在类型。即。 int,

double等(地图上的内容<字符串,Twhere T是

模板参数)


To保持值类型我有类似的东西:


模板< class T>

struct数据

{

T值;

}


现在我正在尝试声明如下地图:


std :: map< std :: string,Data> ;;


这不会/不会工作,我不知道怎么声明它?


如果我有一个方法必须将此地图作为参数传递,我将如何使用


例如Foo(std :: map< string,Dataparam)不起作用,我不希望

它要么


有人可以请救命!
Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)

To hold the value type I have something like:

template<class T>
struct Data
{
T value;
}

Now I am trying to declare a map as below:

std::map<std::string, Data>;

this doesn''t/wouldn''t work and I don''t know how to declare it??

if I have a method which has to pass in this map as a parameter, how
would I do it??
e.g Foo(std::map<string, Dataparam ) doesn''t work and I don''t expect
it to either

Can someone please help!



我认为你可以使用


std :: map< std :: string,Data *> ;;

I think you can use

std::map<std::string, Data*>;


这篇关于包含字符串键和模板化值的map ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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