STL 1: History and component Overview

Source: Internet
Author: User
Tags constant definition

--STLThe advantagesC ++Not familiar to developersSTLNo matter what you say, there is no excuse. Instructor Hou Jie's masterpiece 《STLSource code analysis is indeed a deep learningSTLSo I decided to take a good look at this book and record what I learnedBlogAs a learning note, you may still be able to learn from many people in the same path.

If you can learn moreSTLIt can not only be more skillful in application, master a development tool, but also learn its design ideas and apply it, so that your skill can be further divided into layers.

I STLBrief Introduction

C ++ By Bjarne stroustrup Founded in 1979 Year, and that year Alexander
Stepanov Created STL The master's vision is always so advanced. 1993 Year 9 Month STL To enter C ++ The history of the standard library 1998 Year 9 Month STL Finally officially entered C ++ Standard to become C ++ A major faction of the library. Even C ++ Original Library Stream And String Both Template I wrote it again to see its influence (I often see String Should be followed by post-entry Criticism STL It also has a relationship ).

STLDifferent versions are available, includingHPVersion (allSTLSource ),P. J. plaugerVersion (Visual c ++This is used ),Rouge WaveVersion (C ++ BuilderUsed) andSGIVersion. Instructor Hou Jie recommendedSGIIs more readable ,《STLSource code analysis is also based on it.

IISTLSix Components

Learning and usingSTLIs relatively simple, but if you wantSTLThe bottom layer is the iceberg on the bottom of the ocean. Its complexity is self-evident. I hope that people will not be intimidated. The good news is that this is also a treasure of surprises.

Let's take a look at these six components. Here we first have a basic concept. When we use them, let's explore in detail what is going on.

1.Container(Containers),STLThere are many containers, suchList,
Vector, deque, Ma

PAnd containers are used to organize and store data. You can evenInt A [20]The declared array is considered as an integer container, which is stored in linear correlation mode.20Integer Data.STLIsClass Template.

2.ShowAlgorithm(Algorithms)For exampleSort, searchAnd other common algorithms,STLThe algorithm in is equivalentFunction
Template.

3.Iterator(Iterators ),With the container, you need to know how to access the elements in the container. Just like an array that can be accessed using indexes, the iterator is the interface for accessing the container, instead, it will not sell the internal data organization method of the container. AllSTLContainers must come with their own iterators-after all, only the container designer knows how to access the elements of the container.

4.Imitation letter(Functors)The anti-function is a type of overload.Operator ()OfClassOrClass
TemplateWhich is confusing.

5. adapter (adapters) it is a container or anti-function-encapsulated object that only provides required interfaces as needed, instead of providing all the original interfaces, consider designpattern adapter The mode is the same. For example, STL queue and stack although like a container, but it is actually only an adapter, because its underlying layer is completely reused deque .

6.Configurator(Allocators), Responsible for space allocation management, inSTLIs alsoClass. The benefit of separating the configurator into a component is that you can customize your own configurator as needed, so that you can useS

TLAnd can meet the efficiency requirements.

Figure1-1Shows the relationships between the six components.

《STLSource code parsingSTLConstant definition in, and smallProgramTested on various platformsC ++In1.8And1.9.1Section.

 

Figure1-1Interaction between the six components 《STLSource code analysis]

ContainerPassAllocatorObtain the bucket;AlgorithmPassIteratorAccessContainerContent;

AdaptorYesFunctor;FunctorAssistanceAlgorithmComplete different policy changes.

 

1.9We also talked aboutC ++Some confusing syntaxes, temporary objects (mentioned below); static Integer constants (Int, long, Char, shortInClassDirect initialization;STL(Data startsBeginAnd ended inLast-1Location). Here, we only repeat some anti-function content.

3.Function
CallOperator(Operator
())Write speed

Function call operator (C ++In the syntax()Can also be overloaded. If a class is reloaded(), It becomes a form of imitation.

InCIn the language, if you need to pass a function as a parameter, you can only use the pointer method, suchQsort (Function:

Int CMP (const void * a, const void * B ){... }

A A [20];

...

Qsort (A, 20, sizeof (A), CMP );

 

InSTL, These are completed through the Anti-function method, the anti-function method is adaptive (Adaptability) -- That is, some conditions can be added to it to change its state, which is not available in function pointers. The example is as follows:

Template <class T>

Class CCMP {

Public ://Overloaded(), ClassCCMPIt becomes an anti-function.

Int
Operator () (const T & A, const T & B ){... }

};

//Declare an anti-function object

CCMP <A> cmpa;

//Use an anti-function, just like using a function.

If (cmpa (A0, A1) <0 ){

Cout <"A0
<A1 "<Endl;

}

//You can also generate temporary anti-function objects and use

If (CCMP <A> () (A0, A1) <0 ){

Cout <"A0
<A1 "<Endl;

}

CCMP <t>Very closeSTLThe only difference is the adaptability mentioned above.

 

Today is the time to come here. Although it may be written, it will continue tomorrow.

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.