c++ - c ++地图缓存找不到条目
问题描述
我正在尝试编写缓存着色器存储,目前我在地图中使用地图,第一个地图包含该对。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 张地图。
虽然使用地图地图当然是可能的,但仅使用一张地图将有助于避免许多陷阱和错误。
使用 a 定义映射std::tuple
会产生一个具有复合键的映射。
然后按如下方式定义地图:
using UniformLocationCacheMap = std::map< std::tuple<std::string, int>, UniformLocation>;
推荐阅读
- clojure - 如何从 Clojure 中的嵌套映射生成列表?
- javascript - 如何将组件的状态传递给父级?
- ruby-on-rails - 有没有办法 db:seed 在 Rails 中只有一个表?
- html5-canvas - requestAnimationFrame 动画的基本使用
- javascript - 我无法使用 react-native-ble-plx 创建 BleManager 的新实例
- java - 当文本溢出而不是换行时,TextView 会拉伸 CardView
- c++ - 链接节点列表,分段FAULT
- jquery - 声明或语句预期的 jquery 错误
- ios - 在 App Store Connect 销售/分析仪表板上未显示在 App Purchase 中?
- angular - Ngx-Bootstrap Popover 和 Ngx-popover 名称引用冲突