stl地图问题。 [英] stl map questions.

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

问题描述

(使用谷歌翻译)

目前使用如下。

(Use Google Translate)
The currently used as follows.

map<string, Material*> m_mapMaterial;
...
Material* D3D::GetMaterialInMap(string fileName)
{
     if (m_mapMaterial[fileName]) 
          return m_mapMaterial[fileName];
     return NULL;
}



使用密钥获取数据。

相反,可以获得使用数据的密钥? (对于地图< string,> m_mapMaterial;)

或者我是否需要执行以下操作?


Takes the data using the key.
Conversely, there can be obtained a key to use the data? (for the map<string,> m_mapMaterial;)
Or do I need to do the following?

map<string, pair<string, Material*>> m_mapMaterial; // string of map == string of pair



如果你有更好的方法,请告诉我!


Let me know if you have any better way!

推荐答案

在某些情况下,你真的需要不仅通过键找到值,而且还需要键入值。但它只是意味着......价值应该起到关键作用。首先,只有当值也是唯一的时才有可能,否则不知道两个或多个地图元素中的哪一个应该被某个值选择。



To实现这样的功能,你需要定义一个双向,一对一的地图,这可以通过组合两个地图轻松实现,例如: map< string,Material *> map< Material *,string> 。当然,每个由字符串 Material * 对组成的元素应该同时添加到两个地图中,并且应该检查唯一性。此外,您应该考虑 Material 部分的身份:如果由于某种原因,您尝试将两个不同的指针添加到相同的材料 object,从地图功能的角度来看,它们将表现为两个不同的对象。



-SA
In some cases, you really need to find not only the value by the key, but key by value. But it simply means that… the value should play the role of the key. First of all, it's only possible if the values are also unique, otherwise it is unknown which of the two or more map elements should be chosen by some value.

To achieve such functionality, you need to define a "two-way, one-to-one map", which can be easily implemented by composing two maps, for example: map <string, Material*> and map <Material*, string>. Naturally, each element composed of the string and Material* pair should be added in two maps at the same time, and uniqueness should be checked up. Also, you should taken about identity of Material part: if, by some reason, you try to add two different pointers to the same Material object, they will behave as two different object, from the standpoint of map functionality.

—SA


这篇关于stl地图问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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