简体   繁体   English

std :: vector的第二个参数

[英]Second argument to std::vector

Looking at vector , I realized that I have never used the second argument when creating vectors. 通过查看vector ,我意识到创建向量时从未使用过第二个参数。

std::vector<int> myInts; // this is what I usually do
std::vector<int, ???> myOtherInts; // but is there a second argument there?

Looking at the link above it says that it is for: 查看上面的链接,它说是为了:

Allocator object to be used instead of constructing a new one. 要使用的分配器对象,而不是构造一个新的对象。

or, as for this one : 或者, 就此而言

Allocator: Type of the allocator object used to define the storage allocation model. 分配器:用于定义存储分配模型的分配器对象的类型。 By default, the allocator class template for type T is used, which defines the simplest memory allocation model and is value-independent. 默认情况下,使用类型T的分配器类模板,该模板定义了最简单的内存分配模型,并且与值无关。

I guess it has to do with something with memory management. 我想这与内存管理有关。 However, I am not sure how to use that. 但是,我不确定如何使用它。

Any pointers regarding this? 关于此的任何指示?

The default allocator, std::allocator<> , will handle all allocations made by std::vector<> (and others). 默认分配器std::allocator<>将处理std::vector<> (及其他)所做的所有分配。 It will make new allocations from the heap each time a new allocation is needed. 每当需要新分配时,它将从堆中进行新分配。

By providing a custom allocator, you can for instance allocate a big chunk of memory up front and then slice it up and hand out smaller pieces when separate allocations are needed. 通过提供一个自定义分配器,您可以例如预先分配一大块内存,然后将其切成薄片,并在需要单独分配时分发较小的块。 This will increase the allocation speed dramatically, which is good for example in games, at the cost of increased complexity as compared to the default allocator. 与默认分配器相比,这将显着提高分配速度,这在游戏中非常有用,例如,这增加了复杂性。

Some std type implementations have internal stack-based storage for small amounts of data. 一些std类型的实现具有内部基于堆栈的存储空间,用于存储少量数据。 For instance, std::basic_string<> might use what is called a small string optimization , where only strings longer than some fixed length, say 16 characters (just an example!), gets an allocation from the allocator, otherwise an internal array is used. 例如, std::basic_string<>可能使用所谓的小字符串优化 ,其中只有比某个固定长度长的字符串(例如16个字符(仅作为示例!))才从分配器获取分配,否则内部数组为用过的。

Custom allocators are rarely used in general case. 在一般情况下很少使用自定义分配器。 Some examples of where they can be useful: 有关它们可能有用的一些示例:

  • Optimization for a specific pattern of allocations. 针对特定分配模式的优化。 For example, a concurrent program can pre-allocate a large chunk of memory via standard means at the beginning of task execution and then shave off pieces off it without blocking on the global heap mutex. 例如,并发程序可以在任务执行开始时通过标准方式预先分配大块内存,然后将其切掉,而不会阻塞全局堆互斥量。 When task is completed, entire memory block can be disposed of. 任务完成后,可以丢弃整个存储块。 To use this technique with STL containers, a custom allocator can be employed. 若要将此技术与STL容器一起使用,可以使用自定义分配器。

  • Embedded software, where a device has several ranges of memory with different properties (cached/noncached, fast/slow, volatile/persistent etc). 嵌入式软件,其中设备具有具有不同属性(高速缓存/非高速缓存,快速/慢速,易失性/持久性等)的多个内存范围。 A custom allocator can be used to place objects stored in an STL container in a specific memory region. 定制分配器可用于将STL容器中存储的对象放置在特定的内存区域中。

Maybe this will help: http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079 也许这会有所帮助: http : //www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079

You may try google for: stl allocator . 您可以尝试使用Google: stl allocator

Allocators (STL) help you to manage memory for your objects in vector class. 分配器(STL)帮助您管理向量类中对象的内存。 you may use the custom allocator for different memory model( etc). 您可以将自定义分配器用于不同的内存模型(等)。

嗨,您可以找到自定义分配器的示例http://www.codeproject.com/KB/cpp/allocator.aspx

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

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