STL introduction (introduction to the standard template library)

Source: Internet
Author: User

It took me one night to translate an article about STL.Article. The first time I translated this kind of things, I felt that computer books are still better at the original English version, because many concepts cannot be properly expressed in Chinese (it's just that they can't be said ,:-)). Thanks to the first translation, the level must be relatively good. The key is that STL has never been seen before and is unfamiliar, so it is very hard to translate. It is still good to read the original English version, and it is too hard to translate.

STL introduction (introduction to the standard template library)

Gshine Translation

STL (Standard Template Library) is a container class,Algorithm(Algorithm) and iterators C ++ class libraries. It provides many basic algorithms and data structures in computer science. STL is a generic (generic) Library, which means that all its components (components) have been parameterized to the greatest extent, basically, all components in STL are a template ). Therefore, before using STL, you must ensure that you have understood how the template in C ++ works.

Container and Algorithm)

Like most class libraries, STL also contains the container class, which is primarily intended to accommodate other objects (usually multiple objects ). These container classes include the following classes (classes): vector, list, deque, set, Multiset, MAP, multimap, hash_set, hash_multiset, hash_map, and hash_multimap. All these classes are template classes, which can be used to hold any object after instantiation. For example, you can use vector <int> like an array in C, but vector <int> saves you the trouble of manually allocating memory dynamically. Example:

Vector <int> V (3); // declare a vector object containing 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 large set of algorithms used to operate data stored in containers. For example, you can use the reverse algorithm to perform reverse operations on elements in a vector.

Reverse (V. Begin (), V. End (); // v [0] = 17, V [1] = 10, V [2] = 7

There are two key points to note when calling reverse. First, this function is a global function, not a member function in a class. Second, it has two parameters instead of one: that is, it operates on elements in a certain range of a container, rather than on the entire container. Of course, in this example, the operated Element range is exactly the entire container v.

The main cause of the above two cases is reverse, which is separated from the container class like other STL algorithms. This means that the reverse function can be used not only for elements in the reverse vector, but also for elements in the reverse list, or even for elements in the reverse C language array. Therefore, the followingProgramIt is also 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];

Like a vector in reverse order, this example uses a range: the first parameter of reverse points to the starting point of this range, the second parameter points to the next element of the last element in the range. This range can be expressed as [A, A + 6). Note that "[" is used before, but ")" is used before, which means that a is the starting point of the range, but a + 6 is not the last vertex of the range, but the next element (")" of the end vertex, indicating that it does not contain ).

Iterators)

In the above example of the reverse C language array, the parameter passed to the reverse function is obviously a double *. So, when you use the reverse function to reverse a vector or list object, what type of parameters should you pass to this function? That is to say, when the reverse function declares, what type is its formal parameter, and what type is returned by V. Begin () and V. End?

The answer is that the form parameter type of the reverse function is the iterator type, which is actually a generalized pointer. The pointer itself is an iterator, Which is why reverse can reverse an array. Similarly, the iterator and const_iterator types are nested in the vector class. In the preceding example, the return values of V. Begin () and V. End () are vector <int >:: iterator. There are also some ierators, such as istream_iterator and ostream_iterator, but they have nothing to do with containers.

Iterators are the main mechanism for implementing algorithm and container separation: algorithms are implemented by templates and parameterized by iterators. Therefore, they do not have to be restricted to a container. For example, imagine how to write an algorithm to implement a linear table to search for elements within a certain range. The algorithm find () used for STL implementation is as follows ():

Template <class inputiterator, class T>

Inputiterator find (inputiterator first, inputiterator last, const T & Value ){

While (first! = Last & * first! = Value) ++ first;

Return first;

}

The find function has three parameters: Two iterators are used to define the search range, and the other is the value to be searched in the range. This function checks Each iterator from start to end and stops searching until it finds the value specified by the parameter value or reaches the end of the range.

First and last are defined as the inputiterator type, which is a template parameter. That is to say, there is actually no type called "inputiterator": when you call the find function, the compiler replaces the type of the real parameter you sent with the original type parameters inputiterator and T. Therefore, if the first two parameters in the passed arguments are int * and the third parameter is int, you are actually calling the following function:

Int * Find (int * First, int * Last, const Int & Value ){

While (first! = Last & * first! = Value) ++ first;

Return first;

}

Concepts and Modeling)

[Translator's note: concept is like an interface, defining functional requirements, and model is like a class to implement interface functions. Therefore, concept and model are basically not translated in the following text, and the original word is directly referenced]

The template function is not just an STL algorithm, but an important question is what type can accurately Replace the template parameters. For example, it is obvious that int * and double * can replace the form template parameter inputiterator of the find function. Of course, int and double cannot replace inputiterator, which is also obvious. The find function uses the * First expression. However, for int and double objects, executing the (*) operation is meaningless. For example, int X; it is not suitable to execute * X on X, and it is not allowed. Therefore, the basic answer is that the find function implicitly defines a system's requirements for parameter types. Only types that meet these requirements can be used. For example, to replace the inputiterator type, you must provide some specific operations: the two objects of this type must be able to compare whether they are equal, and must be able to implement auto-increment operations, in addition, you must be able to perform (*) operations to obtain the objects it points.

The find function is not the only STL algorithm with the preceding requirements: for_each, count, and other algorithms also need these requirements. Since these requirements are very important, we name these series of requirements concept. For example, we call the concept that meets the requirements described above as inputiterator. If a type meets those requirements, we say it follows a concept, or it is a concept example (model ). We call int * a model of inputiterator because int * provides all the required operations for the inputiterator concept.

Concept is not part of the C ++ language. We cannot declare a concept in the program, or declare a specific type as a concept model. However, concept is an important part of STL. With concept, it is easy to isolate interfaces from implementations in programs. The author of the find function only needs to consider the concept interface of inputiterator, rather than all types of concept following inputiterator. Similarly, if you want to use the find function, you only need to ensure that the real parameter you pass to it is a model of inputiterator. This is why the find and reverse functions can accept other types such as lists, vectors, and arrays as parameters. Therefore, the use of concept, rather than specific types of programming, can achieve Software Component reusability and ease of integration.

Refinement)

In fact, inputiterator is a very weak concept, because it only applies a few requirements. An inputiterator must support a subset of pointer operations (must implement auto-incrementing operations, whether pre-or post-operations), but does not require all pointer operations. This is sufficient for the find function, but for other algorithms, their parameters may be required to implement more requirements. For example, the reverse function must be able to auto-increment and auto-Subtract its parameters: it uses the expression -- last. With the help of concept, we say that the reverse parameter must be a model of bidirectionaliterrator rather than inputiterator.

Bidirealialiterrator concept is very similar to inputiterator: It only adds some additional requirements. The models of bidirectionaliterrator is a subset of the models of inputiterator. If a type is a model of bidirectionaliterrator, it is also a model of inputiterator. For example, int * is both a model of bidirectionaliterrator and a model of inputiterator. However, istream_iterator is only a model of inputiterator. It does not meet the requirements of strict bidirectionaliterrator.

For the relationship between inputiterator and bidirectionaliterrator, we call it bidirealialiterrator, a refined refinement of inputiterator ). The refinement between concept is like the inheritance relationship between C ++ classes. We use different naming methods to emphasize the specific application of concept and inherit the actual type.

In addition to the two introduced, there are actually three iterators concept. The five iterator concept are output iterator, input iterator, forward iterator, bidirectional iterator, and random access iterator; forwarditerator is the refinement of inputiterator, and worker is the refinement of forwarditerator, randomaccessiterator is a refinement of bidirectionaliterrator. (Outputiterator is related to the other four concept, but it is not in this refined table. It is not refined by other iterator concept, and other iterator concept is not refined .)

The container class is organized into a concept level table just like the iterator. All containers are the concept model of iner. More refined concept, such as sequence and associatecontainer, is used to describe specific types of containers.

Other parts of STL

If you understand algorithms, iterators, and containers, you can basically understand everything in STL. However, STL also includes components of other types ).

First, STL also includes some basic utilities: these basic concept and functions will be used in different parts of the entire class library. For example, assignable concept requires that the type must implement the value assignment operation and copy constructor. Basically, all STL classes are assignable models, and basically all STL algorithms require that their parameters be assignable models.

Secondly, STL includes some memory allocation and release mechanisms. Allocation operators are very professional and you can safely ignore them for whatever purpose.

Finally, STL contains a large set of function objects, also known as functors. Just as the iterator is the generalization of pointers and the function object is the generalization of functions: You can use a function object as you call a common function. There are many different types of concept associated with function objects, including unary functions (function objects that can accept a parameter) and binary functions (function objects that can accept two parameters ). Function objects are a very important part of generic programming, because they make abstraction not only occur in object types, but also can be applied to functions that operate on these objects.

Source: http://www.sgi.com/tech/stl/stl_introduction.html

Gshine

Yu dlut

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.