《STL源码分析》学习笔记 — STL概论与版本简介


STL的设计是为了提升复用性。其价值在于两方面。就低层次而言, STL带给我们一套极具实用价值的零部件,以及一个整合的组织。这种价值体现在使用的便利和多样性上,直接而明朗。除此之外, STL还带给我我们一个高层次、以泛型思维为基础的、系统化的、条理分明的“软件组件分类学”。从这个角度来看, STL是一个抽象概念库,这些“抽象概念”包括最基础的 AssignableDefault Constructible等等;高阶一点的如 Input IteratorUnary FunctionPredicate等等;更高阶的概念包括 sequence containerassociative container等等。

一、STL六大组件

STL提供六大组件,彼此可以组合套用:
(1)容器(containers):各种数据结构,如vectorlistdeque等,用于存放数据。从实现的角度来看,STL容器是一种类模板。
(2)算法(algorithms):各种常用算法如sortsearchcopy等。从实现的角度看,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 StepanovMeng Lee完成的原始版本,这份原始版本有Hewlett-Packard Company(惠普公司)拥有。每一个头文件都有一份声明,允许任何人运用、拷贝、修改、传播、贩卖这些代码,无需付费,唯一的条件是必须将该声明置于使用者开发的文件中。
开放源代码的观念源自美国人Richard Stallman。他创立自由软件基金会,写下著名的GNU宣言。GNU这个词汇很有意思,代表Gnu is Not Unix。
GNU以所谓的GPLGeneral Public License,广泛开放授权)来保护其成员:使用者可以自由阅读与修改GPL软件的源代码,但如果使用者要传播借助GPL软件而完成的软件,他们也必须同意GPL规范。这种精神主要是强迫人们分享并回馈他们对GPL软件的改善。
GPL对于版权观念带来巨大的挑战,甚至被称为“反版权”(copyleft)。GPL带给使用者强大的道德束缚力量,导致各种不同的方对一件。于是,其后又衍生出Library GPLLesser GPLApache LicenseBSD 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公司开发了CygwinCygwin的主要目的是通过重新编译,将POSIX系统(例如LinuxBSD,以及其他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采用。在gccinclude目录中(相对于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                 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值