forward_list,设置,列表等如何调用标准::分配器
How do forward_list,set,list etc call std::allocator?
我注意到分配器只能分配 T
类型的对象并保留大小为 n * sizeof(T)
的内存块。但是,std::list<T>
类型中的链表节点不一定是 T
类型的对象,也不一定与T
对象的大小相同。在这种情况下,std::list
如何使用std::allocator
来分配内存?
这就是重新绑定类型存在的原因。它允许您创建一个类似的分配器,而不是分配其他内容(例如node<T>
)。
基本上是这样的:
std::allocator<int> int_alloc;
std::allocator<int>::rebind<node<int>> node_alloc;
//Perhaps more useful:
decltype(int_alloc)::rebind<node<int>> node_alloc;
当然,在真实情况下,这一切都会被模板化,但希望这能表明这个想法。
有关更多信息,请阅读此处的注释和示例。
相关文章:
- 将手写循环转换为标准库调用
- C++:带有大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?
- 该标准是否说明了例外和不同调用约定的共存
- 在 c++ 中创建 dll 并在 delphi 中调用的标准方法
- C++ 线程时出错,标准::调用:
- 在使用标准向量函数时引发'std::bad_alloc'实例后调用的终止
- 在 MASM 中调用标准库函数
- 从 ASM 调用C++代码中标准库的链接
- 如何查看我从 c++ 标准库中调用了哪些函数?
- 包装标准::线程调用函数
- C 标准的哪一部分涵盖通过空指针调用方法
- C++17标准对在nullptr上调用delete有何规定
- 重定向标准输出时调用 dup2 失败
- 是否可以保证C++标准库容器调用可替换的新功能
- 修复此标准::异步调用
- 生成标准输出仅在 4098 字节输出后调用
- 通过引用查找调用范围的标准引用是什么
- forward_list,设置,列表等如何调用标准::分配器
- 调用标准::排序
- 如何从模板化的流操作符中调用标准流操作符