首页 > 解决方案 > 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;
        }

标签: c++dictionarysearch

解决方案


使用单个地图

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

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

使用 a 定义映射std::tuple会产生一个具有复合键的映射。

然后按如下方式定义地图:

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

推荐阅读