简体   繁体   English

STL容器,哪个合适?

[英]STL Container, which is suitable?

I want to be able to store ID3DXFonts in an STL container, this needs to be done as some of the program will be made in a scripting language. 我希望能够将ID3DXFonts存储在STL容器中,这需要完成,因为某些程序将使用脚本语言编写。 However other parts of it need to be hard coded to be unmolested. 但是,它的其他部分需要进行硬编码才能不受干扰。

I need to be able to workout what font, is needed to render each item on the screen. 我需要能够锻炼在屏幕上呈现每个项目所需的字体。 Weather it hard coded or programed by a script. 可以通过脚本对其进行硬编码或编程。

I'd like to know which STL container would be suitable for this, as when it comes to time to draw the elements on screen. 我想知道哪个STL容器适合于此,因为需要在屏幕上绘制元素。 All the fonts may not have to be used, and I would need to pull say the nth element from the list. 可能不必使用所有字体,我需要从列表中拉第n个元素。

As for tagging a font to a string of text I was thinking about using a template to hold the text and the font ID or placement from the STL. 至于将字体标记为文本字符串,我正在考虑使用模板来保存文本和STL中的字体ID或位置。

There are two steps to the answer: 答案有两个步骤:

  1. You should probably use smart-pointers (probably std::unique_ptr , maybe std::shared_ptr , or even one of the dedicated com-pointers to store each interface-pointer. 您可能应该使用智能指针(可能是std::unique_ptr ,也许是std::shared_ptr ,甚至是专用的指针之一来存储每个接口指针。

  2. If you want random-access to a sequence, nothing beats a std::vector , but a std::array . 如果要随机访问序列,没有什么比std::vector std::array If keys are more haphazardly distributed (or not integers), a std::map or even a std::unordered_map might be more appropriate though. 如果键更随意地分布(或不是整数),则std::map甚至std::unordered_map可能更合适。

If you know the index of the font you need, then use std::vector . 如果知道所需字体的索引,请使用std::vector If you have to look up a key, then you might want to use std::unordered_map (which is the standard c++ hash map). 如果必须查找密钥,则可能要使用std::unordered_map (这是标准的c ++哈希映射)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM