Houjie "STL Source Code" --STL Learning Notes

zhaozj2021-02-16  54

This is some of my personal opinions after I read "STL source analysis", because it is necessary to digest a thing, the best way is to write down their own opinions and understand. After the accumulation of time, it will slowly accumulate you to the true sense. This is my first to formally study notes. I used to have some, but it is more zero, which is more fascinated by STL. I hope to get the master's pointing, because I am still not very understanding for allocator and adapter, I also wrote some imitation of Allocator and Adapter, but it is not very clear, sometimes it is written, and it is not necessarily possible. Especially Adapter, I don't understand the meaning and use of the source. I hope everyone will give me a point. All STL masters and newcomers are welcome to point me. Life-long learning! ! !

Note 1: Some of the algorithms, imitation functions, and interfaces:

First, algorithm:

It is a set of template functions. Operate OK based on the purpose we have to operate. In fact, when we look at STL, you often see the template sub (...) {....}, Etc., thinking that INPUTITERATOR or OUTPUTITERATOR is a fixed type, in fact, this is the STL standard and agreed, you It can be written as a Template sub (...) {....}, In fact, these are all agreed things, that is, if you don't want to meet the STL standard, not the STL standard library expansion, you don't care These agreements. What kind of behavior features we have to pay attention to should be provided in the function that should be required, that is, whether iterators should have Operator , Operator -, Operator , Operator-, Operator (), Operator *, Operator->, etc. The behavior of natural pointers. Because the operation of these operators in the algorithm function seems simple, it is actually possible to use the iterator's behavior. such as:

Template

T2 SUM_AMONG (T1 First, T1 Last, T2 Init)

{

For (; first! = Last; first )

INIT = * first;

Return init;

}

My algorithm can also be placed in the STL standard library as a number of elements between the two iterators, similar to Accumulate

Template

T Accumulate (InputITerator First, TIIT)

{

For (; first! = Last; first )

INIT = * first;

Return init;

}

The same features like these two functions are the second writing that people can understand clearly. It is important to use this function that you should remember:

1. Your InputItemrator Class Ierator should have! =, , * These three pointer behavior. Otherwise, you use a lot of mistakes.

2. As a variable (or object), it should have a behavior ability such as =. For example, we use

Vector v (3, 3);

Int S;

S = Accumulate (v.being (), v.end (), s); / / The result should be 9

转载请注明原文地址:https://www.9cbs.com/read-25226.html

New Post(0)