加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – std :: vector的第二个参数

发布时间:2020-12-16 06:00:08 所属栏目:百科 来源:网络整理
导读:看着 vector,我意识到,在创建向量时,我从来没有使用过第二个参数. std::vectorint myInts; // this is what I usually dostd::vectorint,??? myOtherInts; // but is there a second argument there? 看看上面的链接,它表示它是: Allocator object to be us
看着 vector,我意识到,在创建向量时,我从来没有使用过第二个参数.
std::vector<int> myInts; // this is what I usually do
std::vector<int,???> myOtherInts; // but is there a second argument there?

看看上面的链接,它表示它是:

Allocator object to be used instead of constructing a new one.

或者,至于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.

我想这与内存管理有关.但是,我不知道如何使用它.

任何关于这个的指针?

解决方法

默认分配器std :: allocator将处理由std :: vector<> (和别的).每当需要新的分配时,它将从堆中创建新的分配.

通过提供自定义分配器,您可以例如在前面分配一大块内存,然后在需要单独分配时将其分片并发出较小的内存.这将大大增加分配速度,这在游戏中是比较好的,代价是与默认分配器相比增加了复杂性.

一些std类型的实现具有用于少量数据的内部基于堆栈的存储.例如,std :: basic_string<>可能会使用所谓的小型字符串优化,其中只有长度长于一些固定长度的字符串,比如16个字符(只是一个例子!)从分配器获取一个分配,否则使用一个内部数组.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读