STL generic programming and design new thinking 1

xiaoxiao2021-03-06  77

STL generic programming and design new thinking

Foreword always remember, the purpose of writing code is to make it clear, do not use the language of the language, play smart, and important is to write your understanding code, understand the code you write, so you may do better. --- Herb SUTTER

In 1998, the international C standard was officially passed, and the standardization of C is the most important contribution to "powerful abstraction concept" gives more powerful support to reduce the complexity of software, and C provides two powerful abstract methods: Object-oriented programming and generic programming. Object-oriented programming, you must be very familiar, here no longer. Remove generic programming, some people may not be familiar, but mentioned STL, you will have to hear. STL (Standard Template Library) is actually the implementation of generic programming, STL is developed by Alexander Stepanov (Father), David R Musser and Meng Lee, is incorporated into C standard procedures in 1994 Library. Although STL is relatively late to join the C standard library, it is the most revolutionary part of the C standard library, and is also the most important part of the C standard library. Because almost everything in the new C standard library is made by template, of course, STL will not exceed So, there is a need to first explain the concept of the template. Template concept With a template to make the program better code reuse. Remember, the template is reused the source code, not by inheriting and combining reuse object code, when the user uses templates, the parameters are replaced by the compiler. The template consists of two parts of the class template and function template, with the description of the data type as the parameter is called class template, and the description of the data type is called a function template. Template parameters can be composed of type parameters or non-type parameters. The type parameters can be specified by Class and TypeName keywords. The two senses are the same, which means that the back parameter name represents a potential built-in or user-defined type, non-type parameters A normal parameter declaration composition. Here is a simple usage of class templates and function templates:

Template class queue // class template, where t1 is type parameters, size is non-type parameters {public: explicit queue (): size_ (size) {}; // Explicit constructor, avoid implicit Conversion ... Template Void Assign (T2 First, T2 Last); // Embedded Function Template Private: T * Temp_; int size _;} ///classic function template COMPARE peripheral implementation (eg Queue Category Implementation) Template Template Void Queue :: Assign (T2 First, T2 Last) {}; // Template How to use INT IA [4] = {0, 1, 2, 3}; Queue Qi; Qi.Assign (Ai, Ai 4);

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

New Post(0)