Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(Use Google Translate)
The currently used as follows.
C++
map<string, Material*> m_mapMaterial;
...
Material* D3D::GetMaterialInMap(string fileName)
{
     if (m_mapMaterial[fileName]) 
          return m_mapMaterial[fileName];
     return NULL;
}

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?
C++
map<string, pair<string, Material*>> m_mapMaterial; // string of map == string of pair

Let me know if you have any better way!
Posted
Comments
CPallini 7-Sep-14 7:12am    
What do you mean with: "Conversely, there can be obtained a key to use the data?" ? The map provides iterators to access its items (both key and value).
무명 7-Sep-14 7:50am    
- No key value.
- Data values ​​exist.
In this situation, seek the key value in the map.
CPallini 7-Sep-14 7:57am    
In a map< string key material * pvalue > if you need the key corresponding to pvalue then you might either:
- iterate over all the keys in order to find the item having the given pvalue
or
- use another map, namely map< Material, string> to perform the inverse look-up.
무명 7-Sep-14 8:37am    
As compared with the second method, the following methods are not good?
map < string, pair < string, Material* > >


[use another map, namely map <material, string=""> to perform the inverse look-up.]
If I use the above method ...

map < string, Material* > a;
map < Material*, string > b;
...
string GetString(Material* pMaterial)
{
      return b[pMaterial];
}
void SetMap(Material* pMaterial, string str)
{
      set a, b;
}

It seems confusing.
Sergey Alexandrovich Kryukov 7-Sep-14 11:20am    
Please, why would you need to obtain a key from data? You could if you stored key in data, but why storing double data? I don't see where would you really need it.
—SA

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900