Skip to content

Design and implementation STL

wikipedia Standard Template Library

https://en.wanweibaike.com/wiki-C%2B%2B_Standard_Library

It provides four components called algorithms, containers, functions, and iterators.

NOTE: 参见下面的章节

The STL achieves its results through the use of templates. This approach provides compile-time polymorphism that is often more efficient than traditional run-time polymorphism.

The STL was created as the first library of generic algorithms and data structures for C++, with four ideas in mind:

NOTE: STL的设计理念。

1、generic programming,

2、abstractness without loss of efficiency,

3、the Von Neumann computation model, and

NOTE:

这不理解

4、value semantics.

NOTE:

pass by value

Composition

NOTE:

1、composition能够实现N * M code reuse

2、下面展示了composition

stroustrup C++ in 2005

Containers

Iterators

Algorithms

Functions

NOTE: 在本文中,所谓的“function”,其实就是functor。

Criticisms

Other issues

NOTE: 原文的这一段是非常值得一读的,其中所描述的问题,其实是很多programmer都非常任意范的

1、Initialization of STL containers with constants within the source code is not as easy as data structures inherited from C (addressed in C++11 with initializer lists).

2、STL containers are not intended to be used as base classes (their destructors are deliberately non-virtual); deriving from a container is a common mistake.

NOTE: 这一点需要注意

3、The concept of iterators as implemented by STL can be difficult to understand at first: for example, if a value pointed to by the iterator is deleted, the iterator itself is then no longer valid. This is a common source of errors. Most implementations of the STL provide a debug mode that is slower, but can locate such errors if used. A similar problem exists in other languages, for example Java. Ranges have been proposed as a safer, more flexible alternative to iterators.

4、Certain iteration patterns do not map to the STL iterator model. For example, callback enumeration APIs cannot be made to fit the STL model without the use of coroutines, which are platform-dependent or unavailable, and will be outside the C++ standard until C++20.

NOTE:

stroustrup C++ in 2005

Stroustrup的这篇文章介绍地也比较好。

geeksforgeeks The C++ Standard Template Library (STL)

img

Flowchart of Adaptive Containers and Unordered Containers

img

Flowchart of Sequence conatiners and ordered containers

Implementations

本章描述的是C++标准库的实现,其实它和language的implementation是对应的,显然,它有:

implementation
llvm-project/libcxx
gcc/libstdc++-v3
microsoft/STL

TO READ

http://www.stroustrup.com/DnE2005.pdf

http://www.josuttis.com/libbook/

https://www.eventhelix.com/RealtimeMantra/Patterns/stl_design_patterns.htm

https://justinmeiners.github.io/sgi-stl-docs/design_documents.html