《STL源码分析》学习笔记 — STL概论与版本简介
《STL源码分析》学习笔记 — STL概论与版本简介
- 一、STL六大组件
- 二、GNU源代码开放精神
- 三、HP实现版本
- 四、SGI STL实现版本
- 1、文件分布
- 2、SGI STL 文件分布与简介
- (1)特定版本的C++头文件
- (2)其他目录
- (3)配置文件
- 3、编译器组态设置
STL的设计是为了提升复用性。其价值在于两方面。就低层次而言, STL带给我们一套极具实用价值的零部件,以及一个整合的组织。这种价值体现在使用的便利和多样性上,直接而明朗。除此之外, STL还带给我我们一个高层次、以泛型思维为基础的、系统化的、条理分明的“软件组件分类学”。从这个角度来看, STL是一个抽象概念库,这些“抽象概念”包括最基础的 Assignable、 Default Constructible等等;高阶一点的如 Input Iterator、 Unary Function、 Predicate等等;更高阶的概念包括 sequence container、 associative container等等。
一、STL六大组件
STL提供六大组件,彼此可以组合套用:
(1)容器(containers):各种数据结构,如vector,list,deque等,用于存放数据。从实现的角度来看,STL容器是一种类模板。
(2)算法(algorithms):各种常用算法如sort,search,copy等。从实现的角度看,STL算法是一种函数模板。
(3)迭代器(iterators):扮演容器与算法之间的胶合剂,是所谓的泛型指针。从实现的角度看,迭代器是一种将operator*,operator->,operator++,operator– 等指针相关操作予以重载的类模板。所有STL容器都附带有自己专属的迭代器。只有容器设计者才知道如何遍历自己的元素。原生指针也是一种迭代器。
(4)仿函数(functors):行为类似函数,可作为算法的某种策略。从实现的角度来看,仿函数是一种重载了operator()的类或类模板。一般函数指针可视为狭义的仿函数。
(5)适配器(adapters):一种用来修饰容器、仿函数或迭代器接口的东西。例如,STL提供的queue和stack,虽然看似容器,其实只能算是一种容器适配器,因为它们的底层完全借助deque,所有操作都有底层的deque提供。改变仿函数接口这,被称为fucntion adapter;改变容器接口这,称为container adapter;改变迭代器接口者,被称为iterator adaptor。
(6)配置器(allocator):负责空间配置与管理。从实现的角度来看,配置器是一个实现了动态空间配置、空间管理、空间释放的类模板。
todo组件之间的关系
二、GNU源代码开放精神
全世界所有的STL是想版本,都源于Alexander Stepanov和Meng Lee完成的原始版本,这份原始版本有Hewlett-Packard Company(惠普公司)拥有。每一个头文件都有一份声明,允许任何人运用、拷贝、修改、传播、贩卖这些代码,无需付费,唯一的条件是必须将该声明置于使用者开发的文件中。
开放源代码的观念源自美国人Richard Stallman。他创立自由软件基金会,写下著名的GNU宣言。GNU这个词汇很有意思,代表Gnu is Not Unix。
GNU以所谓的GPL(General Public License,广泛开放授权)来保护其成员:使用者可以自由阅读与修改GPL软件的源代码,但如果使用者要传播借助GPL软件而完成的软件,他们也必须同意GPL规范。这种精神主要是强迫人们分享并回馈他们对GPL软件的改善。
GPL对于版权观念带来巨大的挑战,甚至被称为“反版权”(copyleft)。GPL带给使用者强大的道德束缚力量,导致各种不同的方对一件。于是,其后又衍生出Library GPL,Lesser GPL,Apache License,BSD License等。它们的共同原则就是开发源代码。1998年,自由软件社群企图创造出一个新名词open source来整合各方。open source的定义有10条,任何软件只要符合这10条,就可称呼自己为open source软件(书中提到有9条,截止我写博客的时候查阅网址一共有10条)。GNU GPL也是open source的一种。open source网站中提供了不同种类版权的license样例。
书中使用的环境是Cygnus c++ 2.91 for Windows。之后Cygnus公司开发了Cygwin。Cygwin的主要目的是通过重新编译,将POSIX系统(例如Linux、BSD,以及其他Unix系统)上的软件移植到Windows上。我们这里使用Cygwin 3.2.0版,安装的GCC套件是gcc 10.2.0。此版本的gcc已经支持C++20中的大部分功能。
三、HP实现版本
HP实现版本是所有STL版本的始祖。每一个HP STL头文件都有如下一份声明,允许任何人免费试用、拷贝、修改、传播、贩卖这份软件及其说明文件,唯一需要遵守的是,必须在所有文件中加上HP的版本声明和运用权限声明。这种授权并不属于GNU GPL范畴,但属于open source范畴。
/*** Copyright (c) 1994* Hewlett-Packard Company** Permission to use, copy, modify, distribute and sell this software* and its documentation for any purpose is hereby granted without fee,* provided that the above copyright notice appear in all copies and* that both that copyright notice and this permission notice appear* in supporting documentation. Hewlett-Packard Company makes no* representations about the suitability of this software for any* purpose. It is provided "as is" without express or implied warranty.*/
四、SGI STL实现版本
SGI版本由 Silicon Graphics Computer Systems,Inc. 公司发展,继承HP版本。所以它的头文件也都有HP版本的声明。此外还加上SGI的公司版权声明。从其声明可知,它属于open source的一员,但不属于GNU GPL。
/*** Copyright (c) 1996* Silicon Graphics Computer Systems, Inc.** Permission to use, copy, modify, distribute and sell this software* and its documentation for any purpose is hereby granted without fee,* provided that the above copyright notice appear in all copies and* that both that copyright notice and this permission notice appear* in supporting documentation. Silicon Graphics makes no* representations about the suitability of this software for any* purpose. It is provided "as is" without express or implied warranty.*/
SGI版本被gcc采用。在gcc的include目录中(相对于cygwin的安装目录,其相对路径为 .\lib\gcc\x86_64-pc-cygwin\10\include\c++,我们下面的目录都以此目录为根目录)找到所有STL文件。,并获准自由公开它或修改它甚至贩卖它。作者提到,这个版本的可读性非常高。SGI STL中的部分文件也采用GPL授权方式(如complex.h文件)。
// Copyright (C) 2007-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
1、文件分布
目录 .\lib\gcc\x86_64-pc-cygwin\10\include\c++ 中的头文件和目录([]中的)共102个,共1553819 bytes。
algorithm any array atomic [backward]
bit [bits] bitset cassert ccomplex
cctype cerrno cfenv cfloat charconv
chrono cinttypes ciso646 climits clocale
cmath codecvt compare complex complex.h
concepts condition_variable coroutine csetjmp csignal
cstdalign cstdarg cstdbool cstddef cstdint
cstdio cstdlib cstring ctgmath ctime
cuchar cwchar cwctype cxxabi.h [debug]
[decimal] deque exception execution [experimental]
[ext] fenv.h filesystem forward_list fstream
functional future initializer_list iomanip ios
iosfwd iostream istream iterator limits
list locale map math.h memory
memory_resource mutex new numbers numeric
optional ostream [parallel] [pstl] queue
random ranges ratio regex scoped_allocator
set shared_mutex span sstream stack
stdexcept stdlib.h stop_token streambuf string
string_view system_error tgmath.h thread [tr1]
[tr2] tuple type_traits typeindex typeinfo
unordered_map unordered_set utility valarray variant
vector version [x86_64-pc-cygwin]
子目录 [backward] 中共8个文件,101192 bytes:
auto_ptr.h backward_warning.h binders.h hash_fun.h hash_map
hash_set hashtable.h strstream
子目录 [bits] 中共137个文件,3796862 bytes:
algorithmfwd.h alloc_traits.h allocated_ptr.h allocator.h
atomic_base.h atomic_futex.h atomic_lockfree_defines.h basic_ios.h
basic_ios.tcc basic_string.h basic_string.tcc boost_concept_check.h
c++0x_warning.h char_traits.h charconv.h codecvt.h
concept_check.h cpp_type_traits.h cxxabi_forced.h cxxabi_init_exception.h
deque.tcc enable_special_members.h erase_if.h exception.h
exception_defines.h exception_ptr.h forward_list.h forward_list.tcc
fs_dir.h fs_fwd.h fs_ops.h fs_path.h
fstream.tcc functexcept.h functional_hash.h gslice.h
gslice_array.h hash_bytes.h hashtable.h hashtable_policy.h
indirect_array.h int_limits.h invoke.h ios_base.h
istream.tcc iterator_concepts.h list.tcc locale_classes.h
locale_classes.tcc locale_conv.h locale_facets.h locale_facets.tcc
locale_facets_nonio.h locale_facets_nonio.tcc localefwd.h mask_array.h
memoryfwd.h move.h nested_exception.h node_handle.h
ostream.tcc ostream_insert.h parse_numbers.h postypes.h
predefined_ops.h ptr_traits.h quoted_string.h random.h
random.tcc range_access.h range_cmp.h ranges_algo.h
ranges_algobase.h ranges_uninitialized.h refwrap.h regex.h
regex.tcc regex_automaton.h regex_automaton.tcc regex_compiler.h
regex_compiler.tcc regex_constants.h regex_error.h regex_executor.h
regex_executor.tcc regex_scanner.h regex_scanner.tcc shared_ptr.h
shared_ptr_atomic.h shared_ptr_base.h slice_array.h specfun.h
sstream.tcc std_abs.h std_function.h std_mutex.h
stl_algo.h stl_algobase.h stl_bvector.h stl_construct.h
stl_deque.h stl_function.h stl_heap.h stl_iterator.h
stl_iterator_base_funcs.h stl_iterator_base_types.h stl_list.h stl_map.h
stl_multimap.h stl_multiset.h stl_numeric.h stl_pair.h
stl_queue.h stl_raw_storage_iter.h stl_relops.h stl_set.h
stl_stack.h stl_tempbuf.h stl_tree.h stl_uninitialized.h
stl_vector.h stream_iterator.h streambuf.tcc streambuf_iterator.h
string_view.tcc stringfwd.h uniform_int_dist.h unique_lock.h
unique_ptr.h unordered_map.h unordered_set.h uses_allocator.h
valarray_after.h valarray_array.h valarray_array.tcc valarray_before.h
vector.tcc
子目录 [debug] 中共33个文件,493051 bytes:
array assertions.h bitset debug.h
deque formatter.h forward_list functions.h
helper_functions.h list macros.h map
map.h multimap.h multiset.h safe_base.h
safe_container.h safe_iterator.h safe_iterator.tcc safe_local_iterator.h
safe_local_iterator.tcc safe_sequence.h safe_sequence.tcc safe_unordered_base.h
safe_unordered_container.h safe_unordered_container.tcc set set.h
stl_iterator.h string unordered_map unordered_set
vector
子目录 [decimal] 中共2个文件,34635 bytes:
decimal decimal.h
子目录 **[experimental]**中共38个文件,442010 bytes:
algorithm any array [bits] buffer
chrono deque executor filesystem forward_list
functional internet io_context iterator list
map memory memory_resource net netfwd
numeric optional propagate_const random ratio
regex set socket source_location string
string_view system_error timer tuple type_traits
unordered_map unordered_set utility vector
二级子目录 [experimental\bits] 中共8个文件,99493 bytes:
fs_dir.h fs_fwd.h fs_ops.h fs_path.h lfts_config.h
net.h shared_ptr.h string_view.tcc
子目录 [ext] 中共43个文件,847390 bytes:
algorithm aligned_buffer.h alloc_traits.h atomicity.h
bitmap_allocator.h cast.h cmath codecvt_specializations.h
concurrence.h debug_allocator.h enc_filebuf.h extptr_allocator.h
functional hash_map hash_set iterator
malloc_allocator.h memory mt_allocator.h new_allocator.h
numeric numeric_traits.h [pb_ds] pod_char_traits.h
pointer.h pool_allocator.h random random.tcc
rb_tree rc_string_base.h rope ropeimpl.h
slist sso_string_base.h stdio_filebuf.h stdio_sync_filebuf.h
string_conversions.h throw_allocator.h type_traits.h typelist.h
vstring.h vstring.tcc vstring_fwd.h vstring_util.h
二级子目录 [ext\pb_ds] 中共8个文件,89713 bytes(此目录下还包含多级目录展开,这里不不一一罗列):
assoc_container.hpp [detail] exception.hpp hash_policy.hpp list_update_policy.hpp
priority_queue.hpp tag_and_trait.hpp tree_policy.hpp trie_policy.hpp
子目录 [parallel] 中共43个文件,537288 bytes:
algo.h algobase.h algorithm algorithmfwd.h balanced_quicksort.h
base.h basic_iterator.h checkers.h compatibility.h compiletime_settings.h
equally_split.h features.h find.h find_selectors.h for_each.h
for_each_selectors.hiterator.h list_partition.h losertree.h merge.h
multiseq_selection.hmultiway_merge.h multiway_mergesort.hnumeric numericfwd.h
omp_loop.h omp_loop_static.h par_loop.h parallel.h partial_sum.h
partition.h queue.h quicksort.h random_number.h random_shuffle.h
search.h set_operations.h settings.h sort.h tags.h
types.h unique_copy.h workstealing.h
子目录 [pstl] 中共22个文件,499891 bytes:
algorithm_fwd.h algorithm_impl.h execution_defs.h execution_impl.h
glue_algorithm_defs.h glue_algorithm_impl.h glue_execution_defs.h glue_memory_defs.h
glue_memory_impl.h glue_numeric_defs.h glue_numeric_impl.h memory_impl.h
numeric_fwd.h numeric_impl.h parallel_backend.h parallel_backend_serial.h
parallel_backend_tbb.h parallel_backend_utils.h parallel_impl.h pstl_config.h
unseq_backend_simd.h utils.h
子目录 [tr1] 中共62个文件,743606 bytes:
array bessel_function.tcc beta_function.tcc ccomplex
cctype cfenv cfloat cinttypes
climits cmath complex complex.h
cstdarg cstdbool cstdint cstdio
cstdlib ctgmath ctime ctype.h
cwchar cwctype ell_integral.tcc exp_integral.tcc
fenv.h float.h functional functional_hash.h
gamma.tcc hashtable.h hashtable_policy.h hypergeometric.tcc
inttypes.h legendre_function.tcc limits.h math.h
memory modified_bessel_func.tcc poly_hermite.tcc poly_laguerre.tcc
random random.h random.tcc regex
riemann_zeta.tcc shared_ptr.h special_function_util.h stdarg.h
stdbool.h stdint.h stdio.h stdlib.h
tgmath.h tuple type_traits unordered_map
unordered_map.h unordered_set unordered_set.h utility
wchar.h wctype.h
子目录 [tr2] 中共6个文件,63779 bytes:
bool_set bool_set.tcc dynamic_bitset dynamic_bitset.tcc ratio
type_traits
子目录 [x86_64-pc-cygwin] 中共2个文件夹,0 bytes:
[bits] [ext]
二级子目录 [x86_64-pc-cygwin\bits] 中共22个文件,169154 bytes:
atomic_word.h basic_file.h c++allocator.h c++config.h c++io.h
c++locale.h cpu_defines.h ctype_base.h ctype_inline.h cxxabi_tweaks.h
error_constants.h extc++.h gthr-default.h gthr-posix.h gthr-single.h
gthr.h messages_members.h opt_random.h os_defines.h stdc++.h
stdtr1c++.h time_members.h
二级子目录 [x86_64-pc-cygwin\ext] 中共1个文件,4756 bytes:
opt_random.h
2、SGI STL 文件分布与简介
gcc源码中涉及了C++头文件目录的简介 —— Directory Layout and Source Conventions 以及 Headers。
所有头文件可以分为两部分,与某个版本的C++相关的头文件,包括根目录中除了ABI的目录(gcc中为std目录)、backward目录以及bits目录;所有其他头文件,包括TS(experimental目录), TR1(tr1目录、tr2目录、decimal目录), C++ ABI(cxxabi.h文件、bits\cxxabi_forced.h文件) 以及Extensions(ext目录、debug目录、parallel目录、pstl目录)。
(1)特定版本的C++头文件
在根目录下,其文件大致可分为两类:
C++标准规范下的C头文件 — 例如cstdio,cstdlib,cstring等等。它们的头文件中都包含了这样一行注释以说明其来源(以cstdio为例):
// This is the C++ version of the Standard C Library header @c stdio.h
C++标准头文件 — 例如vector,deque,list等。标准头文件中都有这样一行注释(该注释在前面所说的C头文件中也有,因为C头文件也是标准C++的一部分):
// This is a Standard C++ Library header
根目录中的文件是可以直接被 include<> 引入的。
子目录 [basckward] 中包含了向前兼容的文件,例如hash_map。这类文件已经不再被使用。
子目录 [bits] 中包含了一些C++标准头文件对应的实现文件,包括STL对应的实现文件,如stl_map、stl_vector等等。
(2)其他目录
TS是 technical specification 的缩写,表示技术规范。这部分功能属于实验性功能,有可能在未来被纳入C++标准中。在2012年以前,C++标准委员会提交的并不是TS,而是TR(technical report)。
子目录 [experimental] 中包含了TS相关的头文件。
子目录 [tr1]、[tr2] 中包含了TR相关的头文件。
子目录 [decimal] 中包含了TR中处理浮点类型和小数的头文件。
ABI(Application binary interface)指的是C++应用程序的二进制接口,用于二进制兼容。Extensions 指的是标准头文件的扩展(函数、类的扩展)。
子目录 [debug] 中包含了调试模式中使用的头文件。
子目录 [ext] 中包含了标准库扩展头文件。理论上任何标准头文件不应该依赖于它们(实际上不是)。
子目录 [parallel] 中包含了多线程相关的头文件。
子目录 [pstl] 中包含了线程中使用的STL头文件。
(3)配置文件
子目录 [x86_64-pc-cygwin] 中包含了针对移植目标架构的CPU、IO、操作系统、C++功能等的配置文件。类似地,mingw 的配置文件保存在 [i686-w64-mingw32] 目录中。gcc本身支持多种架构,因此其配置文件单独保存在 [include] 目录同级的 [config] 目录。
3、编译器组态设置
cygwin 中的编译器组态设置保存在目录 [x86_64-pc-cygwin\bits\c++config.h] 中。与书中不同,目前我们已经完全支持 STL 中的标准功能。因此书中提到的 stl_config 文件已经去掉,且在c++config中不含单线程的STL相关配置。
《STL源码分析》学习笔记 — STL概论与版本简介相关推荐
- FATFS文件系统+源码分析——学习笔记
一.概述 1.目的 在移植之前,先将源代码大概的阅读一遍,主要是了解文件系统的结构.各个函数的功能和接口.与移植 相关的代码等等. 2.准备工作 在官方网站下载了0.12c版本的源代码,利用UE进行阅 ...
- STL源码剖析学习七:stack和queue
STL源码剖析学习七:stack和queue stack是一种先进后出的数据结构,只有一个出口. 允许新增.删除.获取最顶端的元素,没有任何办法可以存取其他元素,不允许有遍历行为. 缺省情况下用deq ...
- STL源码剖析学习二:空间配置器(allocator)
STL源码剖析学习二:空间配置器(allocator) 标准接口: vlaue_type pointer const_pointer reference const_reference size_ty ...
- Apollo源码剖析学习笔记2
Apollo 源码剖析学习笔记2 Talker-ListenerNode 目录中包含了 Node 对象.Reader 对象和 Writer 对象.Node 对象主要对应 Ros 中的 Node 节点, ...
- caffe源码c++学习笔记
转载自:深度学习(七)caffe源码c++学习笔记 - hjimce的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/hjimce/article/details/ ...
- 深度学习(七)caffe源码c++学习笔记
caffe源码c++学习笔记 原文地址:http://blog.csdn.net/hjimce/article/details/48933845 作者:hjimce 一.预测分类 最近几天为了希望深入 ...
- 免费学习机器学习和深度学习的源码、学习笔记和框架分享
机器学习和深度学习的免费学习源码.学习笔记和框架分享 python笔记 源码 python导入模块的的几种方式 在python中,字典按值排序 python中set的基本常用方法 python取出fr ...
- STL源码剖析-第一章STL概论与版本简介
系列文章目录 第一章:STL概论与版本简介 文章目录 系列文章目录 前言 一.STL是什么? 二.STL六大组件 1.引入库 前言 源码之前 了无秘密 这本书不适合C++ 初学者,不适合 Generi ...
- STL源码剖析学习之increment、decrement、dereference实现源码
//STL之increment.decrement.dereference实现源码 //学习目的:STL实现原理.操作符(++i,i++,*等操作符的重载) //increment/dereferen ...
- STL 源码分析之string(一)基础篇
STL源码下载: https://www.sgi.com/tech/stl/download.html vs工程代码:http://download.csdn.net/download/jmh1996 ...
最新文章
- 云计算集成过程中七个关键性的问题
- TimeQuest之delay_fall clock_fall傻傻分不清楚
- android GLSurfaceView渲染模式
- 字符串距离(opj )(动态规划)
- 极目眺望的tiantianguandan
- 还有 2 天,这场大咖云集的启智开发者大会即将启动!
- java 解析xml文件
- defender 报备_微软Defender获AV-TEST杀软评测最高分
- HDU6464 (权值线段树)-(查找区间第k1小于第k2小之间的和)
- Qt的project文件的设置相关
- IntelliJ IDEA Maven安装配置
- win10双系统linux,win10系统linux双系统安装的操作方法
- 小程序behavior
- java 时间段求并集_java多个时间段 互相有交集求并集的问题
- javaScript原生版购物车:全选、单选、全删、商品数量增减、计算总价、添加商品(代码)
- Nginx学习八:虚拟主机
- matlab对图像频谱图分析,应用Matlab对图像信号进行频谱分析及滤波
- 安科瑞数据中心行业电能质量监测与治理方案-李亚俊
- CAD图块无法分解怎么办?CAD块分解教程
- 射频功率计介绍——一款超紧凑的多功能射频仪器
热门文章
- mpeg文件格式分析
- Golang 内存分配之逃逸分析
- 易经占卜的演变与人工取名
- gwipr70驱动天空_win7 kmplayer plus设置_gwi pr2 pluswin7驱动_kmplayerplus官方下载
- 微信声音锁会上传到服务器吗,微信声音锁安全吗?微信声音锁可以换吗?
- 服务器游戏性能测试工具,python 游戏服务器 性能测试工具
- FillRect与FrameRect
- 轻量化安装 TKEStack:让已有 K8s 集群拥有企业级容器云平台的能力
- PHP内裤,用sqlmap来扒下她的内裤吧~
- 怎么查看计算机簇大小,分区格式与簇的大小讲解