list如何分配节点与元素

list如何分配节点与元素

本文介绍了std :: list如何分配节点与元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

std::list如何分配保留next/prev指针和其中包含的T元素的节点?

How does std::list allocate the nodes in which it keeps the next/prev pointers and the T element it contains?

我认为标准分配器只能用于分配一种类型的内存(因为std::allocator::allocatesizeof(T)的增量分配内存).因此,似乎不可能以单一分配方式分配列表节点和所包含的对象,这意味着必须根据实现决定,对节点进行分配,并且节点存储指向对象的指针,而不是对象本身,这意味着从指针到列表节点到它所包含的对象的两个间接访问级别,这似乎效率很低.是这样吗?

I think that standard allocators can only be used to allocate memory for one type (because std::allocator::allocate allocates memory in increments of sizeof(T)). So it seems impossible to allocate the list node and the contained object in a single allocation, which means that the nodes have to be allocated with whatever the implementation decides, and the nodes store pointers to the objects instead of the objects themselves, and this implies two levels of indirection to get from a pointer to a list node to the object it contains, which seems inefficient. Is this the case?

推荐答案

分配器具有成员模板类rebind,该成员模板类负责分配其他类型. std::allocator 此处的页面实际上包含了您的确切身份示例问.我在这里引用它:

The allocator has a member template class, rebind, which is responsible for allocating other types. The page for std::allocator here actually has an example of the exact thing you are asking. I will quote it here:

直到C ++ 11

自C ++ 11起

这篇关于std :: list如何分配节点与元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:11