Standard Template Library - STL - 标准模板库

Standard Template Library - STL - 标准模板库

The Standard Template Library (STL) is a software library for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components called algorithms, containers, functions, and iterators.
标准模板库 (Standard Template Library, STL) 是一个 C++ 软件库,大量影响了 C++ 标准程序库但并非是其的一部分。其中包含 4 个组件,分别为算法、容器、函数、迭代器。

STL has four components

  • Algorithms - 算法
  • Containers - 容器
  • Functions - 函数
  • Iterators - 迭代器

模板是 C++ 程序设计语言中的一个重要特征,而标准模板库正是基于此特征。标准模板库使得 C++ 编程语言在有了同 Java 一样强大的类库的同时,保有了更大的可扩展性。

一个常见的误解是 STL 是 C++ 标准程序库的一部分,但事实上并非如此。

STL 将 在数据上执行的操作要执行操作的数据分开,分别以如下概念指代:

  • 容器:包含、放置数据的地方。
  • 迭代器:在容器中指出一个位置、或成对使用以划定一个区域,用来限定操作所涉及到的数据范围。
  • 算法:要执行的操作。

1. Iterators - 迭代器

迭代器是泛化的指针,通过使用迭代器,开发者可以操作数据结构而无需关心其内部实现。根据迭代器的操作方式的不同,迭代器分为五种:

  • 输入迭代器
  • 输出迭代器
  • 前向迭代器
  • 双向迭代器
  • 随机访问迭代器

2. Algorithms - 算法

STL 提供了一些常见的算法,如排序和搜索等。这些算法与数据结构的实现进行了分离。因此,也可对自定义的数据结构使用这些算法,只需让这些自定义的数据结构拥有算法所预期的迭代器。

3. Adaptor - 适配器

适配器为一个模板类,用于提供接口映射。

3. Containers - 容器

标准模板库包含了序列容器 (sequence containers) 与关系容器 (associative containers)。

The STL contains sequence containers and associative containers. The containers are objects that store data. The standard sequence containers include vector, deque, and list. The standard associative containers are set, multiset, map, multimap, hash_set, hash_map, hash_multiset and hash_multimap. There are also container adaptors queue, priority_queue, and stack, that are containers with specific interface, using other containers as implementation.
STL 包含序列容器 (sequence containers) 与关系容器 (associative containers)。容器是存储数据的对象。标准序列容器包括 vector, deque, and list。标准关系容器包括set, multiset, map, multimap, hash_set, hash_map, hash_multiset and hash_multimap。还有一些容器适配器 queue, priority_queue, and stack,它们是具有特定接口的容器,使用其他容器作为实现。

3.1 Sequences (arrays/linked lists): ordered collections - 序列容器:有序集

vector
a dynamic array, like C array (i.e., capable of random access) with the ability to resize itself automatically when inserting or erasing an object. Inserting an element to the back of the vector at the end takes amortized constant time. Removing the last element takes only constant time, because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.
动态数组,类似 C 语言数组 (即能够随机访问),并具有在插入或删除对象时自动调整自身大小的功能。在向量的尾部插入一个元素需要摊销固定时间。删除最后一个元素需要固定的时间,因为不会调整大小。在开始或中间插入和擦除的时间是线性的。

A specialization for type bool exists, which optimizes for space by storing bool values as bits.
专门针对布尔类型,该类型通过将布尔值存储为位来优化空间。

vector 可以如同数组一样的访问方式,例如使用下标 (operator[]) 运算符,并记得自己的长度信息 (size),您也可以使用对象的方式来访问 vector(push_back、pop_back)。使用 vector 可以轻易地定义多维可调整型数组 (std::vector<std::vector<...> >)。要使用 vector,必须含入 vector 头文件。vector 可在 O(1) 内完成在末尾插入 / 移除元素,但在 vector 中间或开头插入 / 移除元素,则需要消耗 O(n) 时间。

list
a doubly linked list; elements are not stored in contiguous memory. Opposite performance from a vector. Slow lookup and access (linear time), but once a position has been found, quick insertion and deletion (constant time).
双链表,元素不存储在连续内存中。与向量相反的性能。查找和访问速度很慢 (线性时间),但是一旦找到位置,就可以快速插入和删除 (constant time)。

list 容器是一个有序 (ordered) 的数据结构 (循序容器),每个元素中存储着上一个元素和下一个元素的地址 (指针),因此是一个双向链接的链表。与 vector 相比,其元素的访问速度较慢,而在已知元素位置的情况下,插入和删除速度较快。

slist
a singly linked list; elements are not stored in contiguous memory. Opposite performance from a vector. Slow lookup and access (linear time), but once a position has been found, quick insertion and deletion (constant time). It has slightly more efficient insertion and deletion, and uses less memory than a doubly linked list, but can only be iterated forwards. It is implemented in the C++ standard library as forward_list.
单链表,元素不存储在连续内存中。与向量相反的性能。查找和访问速度很慢 (线性时间),但是一旦找到位置,就可以快速插入和删除 (恒定时间)。与双向链表相比,它的插入和删除效率更高,并且使用的内存更少,但只能向前迭代。它在 C++ 标准库中以 forward_list (单向链表) 的形式实现。

deque (double-ended queue) - deque (双端队列)
a vector with insertion/erase at the beginning or end in amortized constant time, however lacking some guarantees on iterator validity after altering the deque.
可看做为能在常量时间内完成向开头或结尾插入或删除元素的 vector,但是修改之后,其迭代器的有效性就无法得到保障。

3.2 Associative containers: unordered collections - 关联容器:无序集

set
a mathematical set; inserting/erasing elements in a set does not invalidate iterators pointing in the set. Provides set operations union, intersection, difference, symmetric difference and test of inclusion. Type of data must implement comparison operator < or custom comparator function must be specified; such comparison operator or comparator function must guarantee strict weak ordering, otherwise behavior is undefined. Typically implemented using a self-balancing binary search tree.
数学集,在集合中插入/擦除元素不会使指向集合的迭代器无效。提供集合运算并集、相交、差、对称差和包含测试。数据类型必须实现比较运算符 < 或必须指定自定义比较器功能。这样的比较运算符或比较器功能必须保证严格的弱排序,否则行为是不确定的。通常使用自平衡二进制搜索树来实现。

不重复元素的集合。

multiset
same as a set, but allows duplicate elements (mathematical multiset).
set 具有相同功能,但允许重复的元素。

map
an associative array; allows mapping from one data item (a key) to another (a value). Type of key must implement comparison operator < or custom comparator function must be specified; such comparison operator or comparator function must guarantee strict weak ordering, otherwise behavior is undefined. Typically implemented using a self-balancing binary search tree.
关联数组,允许从一个数据项 (a key) 到另一个数据项 (a value) 的映射。键类型必须实现比较运算符 < 或必须指定自定义比较器功能,这样的比较运算符或比较器功能必须保证严格的弱排序,否则行为是不确定的。通常使用自平衡二进制搜索树来实现。

关联数组,每个元素含有两个数据项,map 将一个数据项映射到另一个数据项中。

multimap
same as a map, but allows duplicate keys.
map 具有相同功能,但允许重复的键值。

hash_set
hash_multiset
hash_map
hash_multimap
similar to a set, multiset, map, or multimap, respectively, but implemented using a hash table; keys are not ordered, but a hash function must exist for the key type. These types were left out of the C++ standard; similar containers were standardized in C++11, but with different names (unordered_set and unordered_map).
分别类似于 set, multiset, map, or multimap,但使用哈希表实现。它的键 (keys) 没有排序,但是键类型必须存在哈希函数。这些类型被排除在 C++ 标准之外。类似的容器在 C++ 11 中已标准化,但名称不同 (unordered_setunordered_map)。

generic program:泛型编程
function template:函数模板
instance:实例
overloading mechanism:重载化机制
pointer:指针

3.3 Other types of containers - 其他类型的容器

bitset
stores series of bits similar to a fixed-sized vector of bools. Implements bitwise operations and lacks iterators. Not a sequence. Provides random access.
存储一系列类似于固定大小的布尔向量的位。实现按位运算,并且缺少迭代器。没有顺序。提供随机访问。

存储系列位类似的固定大小的布尔向量。实现按位运算,没有迭代器,不是序列。可视为 std::array<bool, N>。若需要改变序列长度,可用 std::vector<bool>

valarray
Another array data type, intended for numerical use (especially to represent vectors and matrices); the C++ standard allows specific optimizations for this intended purpose.
另一种数组数据类型,供数值使用 (尤其是表示向量和矩阵),C++ 标准允许为此目的进行特定的优化。

数值类型的 std::vector。牺牲泛型能力而专为数值计算做了优化,例如在数组上的 sin 操作可对数组内所有数值取正弦。有些实现会对 std::valarray 应用向量指令等优化手段。

一个观点是里面全是数值类型的 valarray 才是数学意义上的向量,而可以泛型的 vector 更该叫 array,编程语言中的数组。

发布了443 篇原创文章 · 获赞 1685 · 访问量 101万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104212341