博客
关于我
STL-空间配置器一 (构造和析构)
阅读量:641 次
发布时间:2019-03-14

本文共 2265 字,大约阅读时间需要 7 分钟。

STL六大组件及空间配置器

在C++编程中,STL(标准板本.library, Standard Template Library)提供了许多强大的工具来辅助开发。其中,六大组件是STL的核心,任何复杂的应用开发都依赖于这些组件的有效运用。

1. 容器(containers)

容器类是STL最基本的工具,它们提供了各种数据结构来存储和操作数据。常见的容器包括:

  • vector:动态数组,内存连续,操作速度快。
  • list:双向链表,插入和删除操作高效,但迭代速度较慢。
  • deque:双端队列,支持高效的前驱和后驱操作。
  • set:有序集合,存储元素唯一且有序。
  • map:哈希表,记录键值对,并且可以保持有序。

这些容器从实现上看都是class template,允许它们支持任意类型的数据。

2. 算法(algorithms)

算法组件为容器提供了基本操作,如排序、查找等。常见算法包括:

  • sort:对容器中的元素进行排序。
  • search:查找特定元素。
  • copy:复制容器中的元素到另一个容器。
  • erase:从容器中删除特定元素。

这些算法都是function template,支持与任意容器进行操作。

3. 迭代器(iterators)

迭代器是容器与算法之间的桥梁,它提供了类似于指针的操作:

  • operator*: 通过迭代器获取当前元素。
  • operator->: 通过迭代器获取指针。
  • operator++: 前进到下一个元素。
  • operator--: 后退到前一个元素。

迭代器是一个class template,专门为不同的容器定义。

4. 仿函数(functors)

仿函数的行为类似于函数,可以作为算法的策略。常见的仿函数有:

  • less_than: 用于比较操作。
  • greater_than: 用于比较操作。
  • plus: 加法操作。
  • multiplies: 乘法操作。

这些仿函数都是class或class template,通过重载**operator()**实现。

5. 适配器(adapters)

适配器用于修饰容器或仿函数的接口,具体应用:

  • queue:基于deque实现,读写操作由底层容器管理。
  • stack:基于deque实现,操作也由底层容器处理。

适配器可以将一个类型转换为另一种类型的接口,支持更多的应用场景。

6. 配置器(allocator)

配置器负责内存的动态配置与管理。传统的内存配置使用newdelete,而STL将其分开:

  • construct(): 用于构造对象,支持placement new,例如:
    void* ptr = std::alloc();std::construct(ptr, object);
  • destroy(): 用于析构对象,例如:
    std::destroy(ptr, object);

配置器的目标是提高效率,将 :/内存分配和对象构造/分开处理,使得代码更加灵活和高效。

配置器的实现细节

在底层实现中,constructdestroy函数是通过placement newdestroy操作实现的。```cpptemplate <class _t1, class _t2>inline void construct(_t1* __p, const _t2& __value) {new(static_cast<void*>(__p)) _t1(__value);}template

inline void construct(_t1* __p) {new(static_cast<void*>(__p)) _t1();}template
inline void destroy(_Tp* __pointer) {__pointer->~_Tp();}

对于迭代器的版本:```cpptemplate 
inline void destroy(_ForwardIterator __first, _ForwardIterator __last) { _Destroy(__first, __last);}template
inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last) { __destroy(__first, __last, __VALUE_TYPE(__first));}

此外,还有针对元素数值类型的判断:

template 
inline void __destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*) { typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor; __destroy_aux(__first, __last, _Trivial_destructor());}template
inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __false_type) { for (; __first != __last; ++__first) { destroy(&*__first); }}

转载地址:http://hiwlz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>