The colon and his students (serial 9)--Generic paradigm

Source: Internet
Author: User
Tags data structures numeric square root

Generic paradigm

The algorithm is ridge, the data is meat; thought is chicken, conclusion is egg--preface

The colon reopened: "You will not often encounter this scenario: over and over again write similar code, the intention to merge it, but for a variety of reasons can not be fulfilled." ”

"Yes, sometimes two functions are almost identical, because some parameters do not match and cannot be merged," said the comma. ”

"There is a programming paradigm that solves this problem by breaking down barriers between different data structures and making your code no longer bloated, which is generic programming." "The tone and rhetoric of the colon is reminiscent of the TV's weight-loss ads," Generic programming, referred to as GP, the basic idea is: the algorithm and its role of the data structure separation, and the latter as far as possible generalization, maximize the implementation of algorithm reuse. This generalization is based on the parametric polymorphism of the template (parametric polymorphism), which is not only more universal and more efficient than OOP based on inherited subtype polymorphism (subtype polymorphism). This cannot be said to be an abnormal number--we know that universality is often at the expense of efficiency. GP is the most famous representative of the STL in C + +, and subsequently absorbed by Java,c#. In addition, some functional languages such as OCaml, Standard ML, Generic Haskell also support GP. ”

The colon writes down two pieces of code--

C + + (Generic programming):

template <typename T>
T max(T a, T b)    // 求出两个数中的较大者
{
    return (a > b) ? a : b;
}

C (macro definition):

#define max(a,b) ((a) > (b) ? (a) : (b))

"It's a common problem to ask for a larger value in two numbers." The colon explains, "for statically typed languages, if the parameter types are different, they cannot be integrated even if the function body is the same." If the language does not support overloading (overload), functions such as Maxint, Maxlong, Maxfloat, maxdouble, and so on, can also appear, redundant and ugly. Although it can be implemented in C with a macro definition, it is not guaranteed to be type-safe, while C + + templates take care of type safety and code reuse, and are not efficiently lost because they are expanded during compilation. More than this, C + + support operator overloading, in addition to the numeric type, all the definition of the ' > ' operation of the data type can call the Max function, is really one of the N, n trend Infinity Ah! ”

The colon side of the said edge gestures, exaggerated tone and gestures make everyone laugh.

The quotation mark questions: "Java All objects are object, all parameters are replaced by object type, is not a generalization?" ”

The colon replied: "First of all, the basic type such as Int,float is not a subclass of object, although Java new Automatic unboxing (autoboxing/unboxing) function, but pay the cost of performance." More importantly, this will inevitably require type coercion, loss of the advantages of static type language, and a great way to bug bugs. This is why Java eventually introduced the template, albeit belatedly. Similarly, common pointer void * in C + + has a type-safe problem. ”

He says: "Generics are good, they seem to be only used in some parts of the technology, do not have the permeability of several previous paradigms." ”

The colon listens to the language, Fanshen writes down several questions on the blackboard--

1. Randomly extracts 10 numbers from an array of integers, and sums the prime numbers in them.

2. Converts the full square of a unordered integer set to its square root

3. List of students who have passed the gate and have scored an average of 70 points from the Student score table

4. In a shaded two-dollar Tree, paint all the red nodes blue.

5. Copies a string from the penultimate character to another string

6. Reads a non-numeric character from the standard input, prints ' Please enter a number ' on the standard output

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.