• Create Account

## return std::map Question[Solved]!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Conny14156  Members

Posted 28 February 2013 - 07:23 PM

Hi,

I just notice something very wierd today, when I try to return the std::map<string,ID3DResourceView*> value with help from map[std::string] I get the ID3D11ResourceView* pointer, but that only works when I return it two example I have in my code

//While this work

/*
This wont, Both is identical std::map<string,ID3D11ShaderResourceView*>, but the diffrent
is that this code down here insert the string instead with a null ID3D11ShaderResourceViewpointer. And I know its that line cause
*/
pt = Model->Texture.Diffuse.Texture[Model->lastMaterial];
Gfx->D3d11DevCon->PSSetShaderResources(0,1,&pt);//<---- Cause the next line here the map
//had changed with one extra material with a null pointer.


### #2noizex  GDNet+

Posted 01 March 2013 - 05:51 AM

Sorry, could you rephrase your problem, because honestly I can't seem to understand what is it about. Attached code doesn't make any sense too, try to put here some meaningful testcase.

### #3Hodgman  Moderators

Posted 01 March 2013 - 07:27 AM

operator[] is a read + write operation. If the specified key doesn't exist, then it is inserted into the map.

If you want to look up an item that might be in the map, without adding it in cases where it doesn't exist, then use find instead of [].

### #4Conny14156  Members

Posted 01 March 2013 - 07:47 AM

Oh, Sorry It was late at night >.< (Around 4 in the morning XD) Let me try it again.

What am trying to do is to avoid loading more than one of each texture file, so I made a std::map wtih the texture name as the key and the value would be a ID3D11ShaderResourceView* that contain the texture.

Each time I "Load" a model with materials it would check if the material would already exist inside the map, to avoid loading multipile  time of the same texture.

if the texture already had been loaded i would get it by calling RequestTexture which looks like this

{
if(TotalTexture.find(textureName) != TotalTexture.end())
{
MessageBox(0,"Request Granted",textureName.c_str(),MB_OK);
TotalTextureCounter[textureName]++;
}
else
{
MessageBox(0,"Request Failed",textureName.c_str(),MB_OK);
}
return false;
}

Where the TotalTexture and TotalTexture looks like

std::map<std::string,ID3D11ShaderResourceView*> TotalTexture;
std::map<std::string,int> TotalTextureCounter;


The ReqeustTexture returns a shader resource which I assign and insert into a seperate class which I would call the "Model", the model class have its own map which looks like the same except the only texture that gets inserted there would be the materials that the model use, the reason why I try to do this is to avoid unnecessary looping inside the bigger map.

This is how I do it inside my LoadModel

Its works great, my Testing does indeed get the same adress as the loaded texture from the map

ID3D11ShaderResourceView* Testing = 0;
for(int i = 0; i < Model->Materials.size();i++)
{
Model->Materials[i].materialName += ".jpg";
if(Gfx->TotalTexture.find(Model->Materials[i].materialName) == Gfx->TotalTexture.end())
{

Gfx->RequestInsertionOfTexture(Model->Materials[i].materialName,Testing);

}
else
{
Testing = Gfx->RequestTexture(Model->Materials[i].materialName);
//Gfx->RequestInsertionOfTexture(Model->Materials[i].materialName,Testing);

}
}


Now when Am trying to render my object, I try to change material depending on what group it is.

So I did something like this

for(int i = 0; i < Model->Total.Group; i++)
{
if(Model->lastMaterial != Model->groupMaterial[i])
{
//First method
Model->lastMaterial = Model->groupMaterial[i];

// */

//Second method Model->lastMaterial = Model->groupMaterial[i]; pt = Model->Texture.Diffuse.Texture[Model->lastMaterial]; Gfx->D3d11DevCon->PSSetShaderResources(0,1,&pt);//<---- Texture

//*/

} Gfx->D3d11DevCon->DrawIndexed ( Model->DrawingIndexIndices.IndexCount[i], Model->DrawingIndexIndices.StartIndex[i], 0 ); }

In which case instead of getting the texture it somehow decided to insert the texture

like this

### #5Conny14156  Members

Posted 01 March 2013 - 07:48 AM

operator[] is a read + write operation. If the specified key doesn't exist, then it is inserted into the map.

If you want to look up an item that might be in the map, without adding it in cases where it doesn't exist, then use find instead of [].

oh, >.<

Took me so long to write my last post without noticing a new reply thanks, will check it asap to see if that is the case!

### #6noizex  GDNet+

Posted 01 March 2013 - 09:01 AM

Woah man, from completly unclear you went into totally detailed Thanks, it didn't have to be SO DETAILED, but props for this.

Seems like Hodgman was able to figure out the problem from that first vague description, and its indeed what happens. Try to stay away from [] operator as its convenient but it will insert element into map if it can't find it. Its probably better to use find() and insert() / emplace() as they have single behavior (unless of course inserting new element if it couldnt find one is what you really need).

### #7Conny14156  Members

Posted 01 March 2013 - 04:03 PM

is there any diffrent in performance effiency between [] and find()? seeing how my map will get pretty big and gets alot of request in short intervalls.

if there is I have the time to try atleast to make it so that this problem wont happend again >.<

### #8SiCrane  Moderators

Posted 01 March 2013 - 04:05 PM

map::find() will generally perform a little better than using operator[] because it's semantics are simpler.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.