C++ 映射缓存找不到条目

c++ Map cache not find entrys

本文关键字:找不到 缓存 映射 C++      更新时间:2023-10-16

我试图编写缓存的着色器存储,目前im使用Map中的Map,第一个映射保存了这对。 map_of_uniformlocations持有 .使用我当前的解决方案,它将始终转到 else 语句,而不是从地图中获取数据,我认为这是因为每次我应该更新它时都会插入一个新的map_of_uniformlocations地图,但现在我坚持如何做到这一点。任何指示将不胜感激

uniformCache = m_UniformCache[shaderID];
//if specified shader uniform can be found
auto uniformLocation = uniformCache.find(name);
if (uniformLocation != uniformCache.end())
{
//this is true for only one of the objects in the map
return uniformLocation->second;
}
else
{
unsigned int uniformGetLocation = glGetUniformLocation(shaderID, name.c_str());
if (uniformGetLocation == -1)
{
return -1;
}
//I think the problem is with this
uniformCache.insert(std::pair<std::string, unsigned int>(name, uniformGetLocation));
m_UniformCache.insert(std::pair<unsigned int, std::unordered_map<std::string, unsigned int>>(shaderID, uniformCache));
return uniformGetLocation;
}

使用单个地图

也许不是您要找的答案,但您应该能够只使用一张地图来解决此问题,而不是使用 2 张地图。

虽然使用地图地图当然是可能的,但只使用一张地图将有助于避免很多陷阱和错误。

使用std::tuple定义映射将生成具有复合键的映射。

然后按如下方式定义映射:

using UniformLocationCacheMap = std::map< std::tuple<std::string, int>, UniformLocation>;