插入特定的数据结构时,我在做错什么

What am I doing wrong when inserting into a specific data structure?

本文关键字:什么 数据结构 插入      更新时间:2023-10-16

我正在使用SDL2库来创建游戏。我正在存储加载 sdl_textures

std::map<const SDL_Texture, std::vector<int[3]>> textures;

是映射的是 sdl_texture 本身。 value x,y,z 坐标的向量,以表示呈现纹理的所有位置。

当我尝试将 std :: Pair 插入结构时,我遇到的问题是:

textures.insert(
    std::pair<const SDL_Texture, std::vector<int[3]>>( 
       SDL_CreateTextureFromSurface(renderer, s), 
       std::vector<int[3]>()
    )
);

其中渲染器 sdl_renderer ,而 s sdl_surface 。IDE,即 Visual Studio 2017 ,标记为不正确:

no instance of constructor "std::pair<_Ty1,_Ty2>::pair 
[with _ty1=const SDL_Texture, _Ty2=std::vector<int[3],std::allocator<int[3]>>]" 
matches the argument list argument types are: 
(SDL_Texture*, std::vector<int[3],std::allocator<int[3]>>)

它显然不知道如何构造 std :: Pair ,但是我不知道为什么,因为我能够在 for loop中构造一个没有错误:

for (std::pair<const SDL_Texture, std::vector<int[3]>> tex : textures) {
}

我认为这与我插入为 value 的非原始化 std :: vector 有关。那是原因吗?如果是这样,是否有解决方法?如果没有,可能是什么问题?

另外,还有更好的方法可以做我要做的事情吗?我要加快速度。

看一下SDL_CreateTextureFromSurface的声明:

SDL_Texture* SDL_CreateTextureFromSurface(/* arguments omitted for brevity */)

特别注意返回类型。是SDL_Texture*。这意味着指向SDL_Texture

接下来看看地图的关键类型:SDL_Texture。这与SDL_Texture*不同。指针(通常(不是隐式转换到其尖头类型的。


您不应该制作SDL_Texture的副本。最简单的解决方案是存储SDL_CreateTextureFromSurface返回的指针:

std::map<SDL_Texture*, std::vector<int[3]>> textures;

这将允许您以后在不再需要使用SDL_DestroyTexture的纹理时发布分配的资源。