Review the first chapter of STL source code analysis

Source: Internet
Author: User

No secret before source code. -- Hou Jie

For a classic book, you can get it again every time you read it:

Chapter 1: STL Introduction

STL design thinking: the coupling of objects is extremely low, the reusability is extremely high, and the library conforms to the Development closed principle.

 

STL value: 1. It brings us a set of useful parts and an integrated organization.

2. It brings us a high-level, systematic, and clear "Software Component Classification" based on generic thinking ".

 

Under the STL interface, any component has the maximum independence, and is glued together with the so-called iterator, or is matched with each other by the adapter, or

That is, the imitation function dynamically selects a policy.

 

Six major components of STL: 1. Containers: various data structures, such as vector, list, deque, set, and map, are used to store data.

2. algorithms (algorithms): common algorithms, such as sort, search, copy, erase, and etc. From the implementation perspective

STL algorithm is a function template.

3. iterators: act as the glue between containers and algorithms. It is a "generic pointer ". Operator *,

Operator->, operator ++, operator -- and other class templates to be overloaded.

4. functors: A behavior similar to a function, which can be used as a policy of an algorithm. Class with operator () reloaded

Or class template. Generally, function pointers can be regarded as narrow-sense imitation functions.

5. adapters: one is used to modify the container, function simulation, and iterator interfaces. For example: queue

And Stack are a container adapter, and all operations are supplied by the underlying deque.

6. configurator: responsible for space configuration and management. Dynamic space configuration, space management, and space release

Class Template

 

Interaction between six STL components: Container acquires data storage space through Allocator; algorithms accesses data through iterator

Container content, functor can help algorithm to complete different policy changes (such as the last parameter of qsor), Adapter

It can be modified or integrated with functor.

The figure is as follows:

 

GNU: GNU is not UNIX is recursion --

 

The first version of STL is the HP version. This book introduces the sgi stl version.

 

Wonderful source code:

template<class Interactor, class T>Interactor find(Interactor first, Interactor last, const T& value) {    while(first != last && *first != value) {        ++first;    }    return first;} #if defined(_STL_NEED_BOOL)    typedef int bool;# define true 1# define false 0#endif

 

Use the pre-closed and post-open interval notation [). Iterator last refers to the next position of the last element"

 

Implementation of the function-like code:

1 template <class T> 2 struct plus {3 t operator () (const T & X, const T & Y) const {4 return X + Y; 5} 6 }; 7 int main () {8 // The following generation of the imitation function object 9 plus <int> plusobj; 10 // use the imitation function below, just like using a common function, 11 cout <plusobj (812) <Endl; // directly generates a temporary object (the first pair of parentheses) of the function ), and call it (second pair of brackets) 13 cout <plus <int> () <Endl; // 10

 

Review the first chapter of STL source code analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.