当前位置:网站首页>What is C + + STL? It is important to master the core of C + + STL!

What is C + + STL? It is important to master the core of C + + STL!

2020-11-10 13:57:11 Muge love programming

What is? STL?

STL(Standard Template Library), Standard template library , It's an industrial strength , efficient C++ library . It is contained in C++ Standard library (C++ Standard Library) in , yes ANSI/ISO C++ The newest and most revolutionary part of the standard . The library contains many basic data structures and algorithms commonly used in the field of computer science . For the vast C++ Programmers provide an extensible application framework , High degree of software reusability .

STL One of the most important features of is the separation of data structure and algorithm . Although it's a simple concept , But this separation does make STL Become very versatile . for example , because STL Of sort() Functions are completely generic , You can use it to manipulate almost any set of data , Including the list , Containers and arrays ;

STL Another important feature is that it's not object-oriented . In order to be universal enough ,STL Mainly depends on templates rather than encapsulation , Inheritance and virtual functions ( polymorphism )——OOP The three elements of . you are here STL You can't find any obvious class inheritance relationships in . It's like a kind of retrogression , But this is what makes STL The components have the underlying characteristics of wide commonality . in addition , because STL It's based on templates , The use of inline functions makes the generated code short and efficient ;

At the logical level , stay STL It embodies the idea of generic programming , Many new terms have been introduced , Like demand (requirements), Concept (concept), Model (model), Containers (container), Algorithm (algorithmn), iterator (iterator) etc. . And OOP(object-oriented programming) The polymorphic (polymorphism) equally , Generics is also a software reuse technology ; From the level of realization , Whole STL It is implemented in a way of type parameterization , This approach is based on an earlier C++ Language features not found in the standard -- Templates (template).


 

STL The composition of

STL There are three core parts : Containers (Container)、 Algorithm (Algorithms)、 iterator (Iterator), Container adapter (container adaptor), Function object (functor), In addition to that STL Other standard components . Generally speaking :

Containers : Things that hold things , A cup for water , The sea of salt water , A classroom with people ……STL The container in is a template class that can hold some data .

Algorithm : It's just pouring water into a cup , Sewage into the sea , Kick people out of the classroom ……STL The algorithm in , It's the way to deal with the data in the container 、 operation .

iterator : A kettle that pours water into a cup , Sewage pipes , The property manager who kicked people out ……STL The iterator in : Objects that traverse the data in the container . When processing data stored in a container , Iterators can move from one member to another . It can move between members in certain containers in a predefined order . For ordinary one-dimensional arrays 、 vector 、 For double ended queues and lists , An iterator is a pointer .

Let's introduce the container in detail 、 Algorithm 、 iterator 、 functor 、 Container adapter .


 

One 、 Containers

Containers are used to store and manage a collection of objects of a certain kind . For example, a fish tank is a container for goldfish . Every container has its advantages and disadvantages . To meet the needs of the program ,STL A variety of container types are available , The container can be arrays or linked lists, Or each element has a special key value .

As STL The most important part of -- Containers , Divided into vectors (vector), deque (deque), surface (list), queue (queue), Stack (stack), aggregate (set), Multiple sets (multiset), mapping (map), Multiple mapping (multimap).


 

Two 、 iterator

Iterators are used to traverse elements of an object cluster . Object clusters can be containers , It could be part of the container .

The main purpose of iterators is to provide a small set of common interfaces for containers . Use this interface , An operation can progress to the next element in the cluster .

Each container provides its own iterator . The iterator understands the internal structure of the container , So it's going to go right . The interface of an iterator is similar to a general pointer . Most of the time , There is no problem understanding it as a pointer ( A pointer is a special case of iterators , It's also an iterator ), however , It can't be done completely .


 

3、 ... and 、 Algorithm

The algorithm is used to deal with the elements in the cluster , You can search for... For different purposes 、 Sort 、 modify 、 Use those elements . All container iterators provide a consistent interface , With the help of iterators , Algorithmic programs can be used in any container .

STL One of the features of is the separation of data and operations . Data is managed by container categories , Operations are defined by customizable algorithms . An iterator acts in between “ adhesive ”, So that the algorithm can interact with the container .

STL Another feature of is that components can operate against any type .“ Standard template library ” The name means “ Any type is acceptable ” The template of , And these types can perform the necessary operations .

stay STL in , Containers can be divided into sequential containers and relational containers , The function of iterator is to traverse all or part of the objects in the container . Iterators can be divided into 5 Species and genera , this 5 The species belong to two types : Bidirectional iterators and random access iterators .

SIL The algorithms provided in include searching for 、 Sort 、 Copy 、 reorder 、 modify 、 Numerical operation, etc .

Some common algorithms are as follows :

for_each();

find();

find_if();

count();

count_if();

replace();

replace_if();

copy();

unique_copy();

sort();

equal_range();

merge();




 

Four 、 functor

functor , Or function object , yes STL One of the six components ; Although the functor is small , But it has greatly expanded the function of the algorithm , Almost all algorithms have a version of the functor . for example , Search algorithm find_if That's right find The extension of the algorithm , The standard search is to find if two elements are equal , But what is equality requires different definitions in different situations , If the address is equal , The address and postcode are the same , Although these definitions of equality are changing , But the algorithm itself doesn't need to change , Thanks to the functor . functor (functor) Also called function object (function object), In fact, it's overloaded () Operator struct, There is nothing special .

STL Contains a lot of functors . A functor can be understood as a general form of a function . For programming , The functor is very important , And there are several constraints . stay C++ In the standard , Function calls generally use pointers , When you need to call a function , Just provide the address of the function . for example :

staticintcmp(int* i,int* j)

{

return(*i - *j);

}

The above code defines a cmp() function , When you need to call this function , Just provide the address of the function . for example :

qsort(a,10,sizeof(int), cmp);

The biggest drawback of this method is low efficiency . To improve efficiency ,STL This paper defines the concept of paraffins . The following code defines a functor , It is characterized by the use of operator Implementation definition .

structthree_mul

{

booloperator()(int& v)

    {

return(v%3==0)

    }

}

By defining operators, you can significantly improve efficiency . for example ,

for_each (myvector.begin (), myvector.end(), three_mul);

5、 ... and 、 Container adapter

A container adapter is a class template that encapsulates a sequence container , It provides some different functions on the basis of general sequence container . It's called an adapter class , Because it can provide different functions by adapting the existing interface of the container .

The container adapter has  3 Kind of , Namely stack、queue、priority_queue:

stack<T>: It's a package deque The adapter class template for the container , The default implementation is a LIFO (Last-In-First-Out,LIFO) Push the stack of .stack The template is defined in the header file stack in .

queue<T>: It's a package deque The adapter class template for the container , The default implementation is a first in, first out (First-In-First-Out,LIFO) Queues . It can be assigned a base container that meets certain criteria .queue The template is defined in the header file queue in .

priority_queue<T>: It's a package vector The adapter class template for the container , The default implementation is to sort the elements , This ensures that the largest element is always in the front queue of the queue .priority_queue The template is defined in the header file queue in .

The adapter class implements some of its own operations based on the basic sequence container , Obviously, you can also add some of your own operations . They offer the advantage of simplifying the common interface , And it improves the readability of the code .

The operations provided by the three container adapters

1.stack

stack<int> s;

stack< int, vector<int> > stk;  // Override the base container type , Use vector Realization stk

s.empty();  // Judge stack Is it empty , Empty return true, Otherwise return to false

s.size();  // return stack The number of elements in

s.pop();    // Delete stack top element , But it doesn't return its value

s.top();    // Returns the value of the element at the top of the stack , But don't delete this element

s.push(item);  // Push new elements at the top of the stack item

2.queue & priority_queue

queue<int> q; //priority_queue<int> q;

q.empty();  // Determines if the queue is empty

q.size();  // Return queue length

q.push(item);  // about queue, Press a new element at the end of the team

              // about priority_queue, Insert elements at the appropriate position based on the priority

//queue only:

q.front();  // Returns the value of the leader element , But don't delete the element

q.back();  // Returns the value of the element at the end of the queue , But don't delete the element

//priority_queue only:

q.top();    // Returns the value of the element with the highest priority , But don't delete the element

See here, are you right about “C++” I have a little new understanding ~

If you like this article , Move your little finger , Pay attention to it ~


 

 

If you want to be a programmer too , Want to master programming quickly , Here for you to share a learning Penguin circle !

There are senior professional software development engineers , Answer all your doubts online ~C++ introduction “so easy”

Programming learning books :


 

Programming learning video :


 

版权声明
本文为[Muge love programming]所创,转载请带上原文链接,感谢