Standard template library STL

zhaozj2021-02-17  82

Standard template library introduces standard template libraries, also called

STL is a C container library, algorithm, and iterator. He offers many basic algorithms and data structures. STL is a

General library, you can fully customize: Almost all STL components are templates. Before you use STL, you must understand the work of the template.

Container and algorithm

Like many class libraries, STL includes container class - can contain classes of other objects. STL contains vector classes, chain classes, two-way queue classes, collection classes, graphics, and more. Each class in them is template, which can contain various types of objects. For example, you can use Vector , just as an array in a regular C language, in addition to the problem of dynamic memory allocation, considering the dynamic memory allocation, for example.

Vector v (3); // Define a vector class with three elements

v [0] = 7;

v [1] = v [0] 3;

v [2] = v [0] v [1]; // v [0] == 7, v [1] == 10, v [2] == 17

STL also contains a lot of algorithms. They subtly handle data stored in the container. You can reverse the elements in the vector, just simply use the REVERSE algorithm.

Reverse (v.begin (), v.end ()); // v [0] == 17, v [1] == 10, v [2] == 7

There are two points to pay attention when calling Reverse. First of all, he is a full-class function, not a member function. Second, he has two parameters, not one: he operates a certain range of elements instead of operating the container. In this example he is active for the entire container V.

The above two points are the same: REVERSE and other STL algorithms, they are general, that is, Reverse can not only reverse the elements of the vector, but also revers down the sequence of elements in the table. Even the array can be operated. The following procedure is legal.

Double a [6] = {1.2, 1.3, 1.4, 1.5, 1.6, 1.7};

Reverse (A, A 6);

For (int i = 0; i <6; i)

COUT << "A [" << i << "] =" << a [i];

This example also uses the range, as an example of the vector above: The first parameter is a pointer to the headed head to operate, and the second parameter is a pointer to the tail. Here, the range is [A, A 6). Maybe you noticed the asymmetry of the range.

Iterator

In the example above the C array, the parameter type of Reverse is Double *. So what is the parameter in a vector or linked list? That is, how is the REVERSE defines the parameters, and what V.Begin () and v.end () returns?

The answer to the question is that the parameters of Reverse are Itertors. He is a summary of the pointer. The pointerself is also an iterator. This is why the reason why the elements in an array can be reversed. Similarly, vector define nested type Iterator and const_iterator. In the above example, the type returned by v.begin () and v.end () is Vector :: Iterator. There are also some iterators such as iStream_iterator and Ostream_IstReerator. They can't be associated with the container at all. The appearance of the iterator makes the algorithm and the separation of the container possible. The algorithm is a template that is dependent on an iterator, so it is not limited to a single container. For example, we write an algorithm to achieve a range of linear lookups. Below is the STL's Find Algorithm.

Template

InputITerator Find (InputITerator First, InputITerator Last, Const T & Value) {

While (First! = Last && * first! = value) first;

Return first;

}

Find has three parameters: two iterator define the range, the third is the value to find. He traversed the range [first, last), processed from head to tail, stopped or stopped after finding it.

First and last are defined as the InputITerator type, and the inputItItItItIterator is a template parameter. That is, there is virtually no real type inputItemrator: When you call Find, the compiler matches the InputItItorator and T by the actual type. If the first implementation in the two parameters of Find is int *, the third implementation is int, then you can think that it is executing the following function.

INT * Find (int * first, int * last, const INT & value) {

While (First! = Last && * first! = value) first;

Return first;

}

Constraints and categories

For a function template (not just STL algorithm), a very important question is: What kind of parameter type is correct to match the template parameter correct? For detailed description, we example: Obviously, int * or double * can replace the Find template parameter inputITITERATOR. And INT or DOUBLE can't do it. Therefore, there is an obvious answer to: Find implicitly defined the requirements for types. Regardless of the type of type, you must provide a certain operation: he must be able to compare the size of the two objects, and he must be able to increase the operation.

Find is not only one STL algorithm with so requiring. For_each and counts and other algorithms must satisfy the above requirements. These requirements are important, so we give him a name: We will call this type of conditions. CONCEPT. We call the above constraints for the input item. If a type meets all requirements, then we say that this type meets this constraint, or he is a category of this constraint (Model). We speak INT * is the Class of Input Iterator, because int * can provide all the operations required by Input Iterator.

Constraints are not part of the C language, you have no way to define a constraint in a program, or define a type of type that is constrained. However, constraints are a very important part of STL. Using the constraint class mechanism allows us to separate the interface from the program implementation in the program. The author of the Find will consider that the interface is only for the Input Iterator instead of all possible data types that meet the constraint. Similarly, if you want to use Find, then you just make sure you pass the parameters, the Class of Input Iterator is unable to participate. This is why Find and Reverse can use other types of other types in List, Vector, and C. Program with constraints (ideas), rather than fixed to a particular type, allowing program reusable components and combining components. Refinement

Input Iterator is actually a fairly weak constraint. In other words, he has only a few requests. An Input Iterator must support a subset of pointer operations (he must be able to add Input Iterator through operator Operator , but do not need to support all pointer algorithms. This is enough for Find, but other algorithms are not like this, they need to meet additional conditions. For example, Reverse is also capable of supporting operators. In terms of terms, the parameters of Reverse should be Bidirectional Iterator rather than Input Iterator.

The constraint of the Bidirectional Iterator is very similar to the constraints of the Input Iterator: he just simply adds a shortest condition. The Class of the Bidirectional Iterator is a subset of the Input Teerator class: each is the data type of the Bidirectional Iterator class is the Class of Input Iterator. For example, int * is the Class of the Bidirectional Iterator, which is also the universal parameters of the Input Iterator. But iStream_iterator is different, he is just the Class of Input Iterator: He cannot "adapt" more stringent Bidirectional Iterator requirements.

Let's describe the relationship between the Input Iterator and the Bidirectional Iterator: Bidirectional Iterator is the refinement of the Input Iterator. The constraint refinement is very elephant in the C class. The main reason we use different words is that refinement is used for constraints rather than actual types.

In addition to the two iterator constraints mentioned above, there are three iterator constraints. These five iterator constraints are: Output Iterator, Input Iterator, Forward Iterator, Bidirectional Iterator, and Random Access Iterator. Forward Iterator is the refinement of Input Iterator, and Bidirectional Iterator is the refinement of Forward Iterator, and the Random Access Iterator is the refinement of the Bidirectional Iterator. (Output Iterator and other four constraints are related, but not the relationship at the Refinement level: he is not the other four refinements, the other four are not his reference.)

The container class and iterator are the same, there are also constrained hierarchical relationships. All containers are Class of Container belongs. For example, SEQUENCE and ASSOCIATIVE Container are specific containers. Other parts of STL

If you understand the algorithm, iterators, containers, then you have learned STL almost. But STL also contains some of the other parts.

First, STL contains several "tools": very basic functionality and concepts that appear in different places in the class library. For example, an Assignable constraint describes the type of copy operation that can assate to the class. Almost all STL classes are the Class of Assignable. Almost all algorithms require their parameters to be ASSIGNABLE's collections.

Second, STL includes allocation and release of the underlying memory. Allocators is very rare, you can almost ignore them.

Finally, STL contains a lot of object functions, also known as functors. Like the general form of an astrulant is a pointer, the object function is a general form of a function. The object function has several different constraints, including UNARY FUNCTION (an object function having only one parameter, such as f (x)) and binary function (a object function with two parameters, such as f (x, y)) . Object functions are important for programming because he acts like an abstract object type.

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

New Post(0)