插入特定的数据结构时,我在做错什么
What am I doing wrong when inserting into a specific data structure?
我正在使用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
的纹理时发布分配的资源。
相关文章:
- C++中deque数据结构的大O是什么?
- 自定义数据结构的优点是什么?
- 用于筛子的最佳数据结构是什么(即一些被划掉的数字列表)?
- 插入特定的数据结构时,我在做错什么
- 在C++中将数据结构表示为字符串的首选方法是什么?
- 在现有数据结构上使用BGL算法需要什么(边和顶点作为矢量<对象*>)?
- 表示图像矩阵的理想数据结构是什么?
- 使用什么数据结构来存储基于游戏对象的二维单元地图?
- 存储大量会话的最有效数据结构是什么?
- 我应该使用什么数据结构来对数据库/表进行建模
- 给定 C# 代码的正确数据结构是什么
- 可用于存储和管理整数集合的最佳C++数据结构是什么
- 如何维护城市道路数据?(我应该使用什么数据结构)
- 抽象数据类型(ADT)和数据结构之间有什么区别
- 在并发数据结构中,什么级别的锁定粒度是好的
- 您将使用什么数据结构来实现字典
- 什么数据结构可以用于在cpp中存储windows目录结构
- 什么数据结构可以在O(log n)时间内找到给定范围内的元素数
- 在C++中,使用什么数据结构来快速存储文本文件,该文件具有可变的行数和可变的子元素数
- 我应该使用什么数据结构来支持插入、删除和随机选择