C ++ Primer study note _ 46_STL analysis (1): Generic programming, what are the six components of STL and STL, and their relationships

Source: Internet
Author: User

C ++ Primer study note _ 46_STL analysis (1): Generic programming, what are the six components of STL and STL, and their relationships

I. Generic programming

1. generic programming (generic programming): process different types of data with the same logic and Algorithm
2. write programs as common as possible
3. Extract algorithms from the data structure to become common
4. The C ++ template lays a key foundation for generic programming.

2. What is STL?

1. STL (Standard Template Library) is an efficient C ++ Library.
2. It contains many basic data structures and algorithms commonly used in the computer science field. It provides a Scalable Application Framework for the majority of C ++ programmers and highly reflects the reusability of software.

3. In terms of logic, STL embodies the idea of generic programming (generic programming)

(1) In this idea, most basic algorithms are abstract, generalized, independent of the corresponding data structure, and used to process different situations in the same or similar way.

4. At the implementation level, the whole STL is implemented in a type parameterized method.

(1) template-based)

(2) templates, generic program design ideas, STL relationships

Templates lay the foundation for generic Program Design

STL is a set of C ++ standard template libraries, reflecting the concept of generic programming. In other words, STL is a product with a successful general programming philosophy.

Iii. Six STL components and their relationships

1. Six STL components

---- Iner (Container) Basic Data Structures

---- Adapter can change a component of the containers, Iterators, or Function object interface (previously, deque is used to implement a new container Stack, called a container Adapter)

---- Algorithm (Algorithm) various basic algorithms such as sort, search... And so on

---- Iterator (Iterator) connects containers and algorithms. The Iterator serves as a bridge between containers and algorithms.

---- Function object)

---- Allocator (distributor)

2. Container algorithm iterator relationship

Containers are data structures, algorithms are logic, and iterators are traversal interfaces.

3. Container

(1) A container class is an object that contains a set of elements or element sets.


(2) seven basic containers:

---- Vector, deque, list, set, multiset, map, and multimap)

(3) The vast majority of Members of the standard container share the same name.

(4) sequential containers

---- Sequence containers, a Sequence container. Each element has a fixed position, which is determined by the insertion time and location, and is irrelevant to the element value. (Vector, deque, list)

(5) associated containers

---- Associative containers, the element location depends on the specific sorting criteria and element values, and is irrelevant to the insertion order. (Set, multiset, map, multimap)

(6) How to Select a sequence container

[1]. You need to insert and/or delete a sequence frequently in the middle of the sequence without having to perform a long-distance jump inside the sequence too much. You should select list. Subscript operations are not supported.

[2]. insertion and deletion of vector headers and intermediary are inefficient, and the insertion and deletion efficiency at the end is high. Supports subscript operations

[3] deque is highly efficient in inserting and deleting headers and tails. Supports subscript operations

4. iterator

(1) Iterators, used to traverse the elements of an object cluster (collection of objects. This object cluster may be a container or a part of the container. The major benefit of the iterator is that it provides a small set of public interfaces for all containers. The iterator repeats with ++ and extracts the result with *. Therefore, it is similar to a pointer. we can regard it as a smart pointer.
(2) For example, the ++ operation can traverse the next element in the cluster. How to do this depends on the internal data organization of the container.
(3) Each container provides its own iterator that can understand the internal data structure of the container.

5. Algorithms

Algorithm Algorithms is used to process elements in a cluster. They can search, sort, modify, and use those elements for different purposes. With the help of the iterator, we can simply write an algorithm and apply it to any container, because all container iterators provide consistent interfaces.

6. Adapter

(1) adapter is an interface class (to construct a new class)

[1] provides new interfaces stack and queue for existing classes

[2] aims to simplify, constrain, secure, hide, or change the set of services provided by the modified class

(2) Three Types of adapters:

[1] container adapter: Used to expand seven basic containers, which are combined with sequential containers to form stacks, queues, and priority queue containers.

[2] iterator adapter (reverse iterator, insert iterator, IO stream iterator)

[3] function adapter (function Object Adapter, member function adapter, and common function adapter)

7. Function objects

(1) function objects are also called functions)
(2) an object with a behavior similar to a function can have no parameters or contain several parameters.
(3) Any object that overload the class that calls operator () satisfies the features of the function object.
(4) function objects can be called smart functions.
(5) STL also defines some standard function objects. Function objects can be divided into arithmetic operations, relational operations, and logical operations. To call these standard function objects, the header file must be included. .

8. Distributor

Space configuration and management. From the perspective of implementation, the Configurator is a class template that implements dynamic space configuration, space management, and space release.

Memory Management after these containers is implemented through a default allocator provided by STL. Of course, you can also customize your Own allocator by implementing the interface method defined in the allocator template, and then passing the custom allocator as a template parameter to the STL container, create an STL container object that uses custom allocator, for example:
Stl: vector Array;
In most cases, the default STL allocator is enough. This allocator is a memory manager composed of two-level distributors. When the applied memory size is greater than bytes, the first-level distributor is started to directly allocate the heap space to the system through malloc, if the requested memory size is less than bytes, the second-level distributor is started to deliver a piece of memory from a pre-allocated memory pool to the user, the memory pool consists of 16 different sizes (multiples of 8, 8 ~ 128 byte). allocator extracts the header block from the corresponding idle block list based on the Applied memory size (which is a multiple of 8.

This method has two advantages:
(1) Fast allocation of small objects. Small objects are allocated from the memory pool. This memory pool is used by the system to allocate a large enough area to the program for backup once malloc is called, when the memory pool is exhausted, apply for a new area from the system. The whole process is similar to wholesale and retail. allocator first wholesalers a certain amount of goods to the general business, and then retail it to the user, it is faster than the process in which a commodity is always sold to the user. Of course, when there is a problem here, the memory pool will cause some memory waste. For example, if you only need to allocate a small object, you may need to apply for a large memory pool for this small object, however, this waste is worthwhile. Moreover, this situation is rare in practical applications.
(2) avoid the generation of memory fragments. The allocation of small objects in the program can easily cause memory fragmentation, which puts a lot of pressure on the memory management of the operating system. The increase of fragments in the system will not only affect the speed of memory allocation, it also greatly reduces the memory usage. The memory pool organizes the memory of small objects. From the system perspective, it is only a large memory pool, and the allocation and release of small object memory cannot be seen.

Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

Related Article

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.