zoukankan      html  css  js  c++  java
  • C++ 泛型编程/模板 泛函编程/Lambda/λ演算

    1、泛型编程(C++模板)

    其中,Ada, Delpha, Java, C#, Swift 称之为 泛型/generics; ML, Scala和 Haskell 称之为 参数多态/parametric polymorphism; C++和D语言称之为 模板/template. 《设计模式/Design Patterns》称之为 参数化类型/parameterized type. 因为在这里,参数的类型在一般情况下都是未知的,而泛型编程可以支持多种类型,所以叫泛/generic

    ①函数模板/Function Template:定义一个函数模板,可以支持多种类型的参数

    The format for declaring function templates with type parameters is:

    template <class identifier> function_declaration;
    template <typename identifier> function_declaration;
    

    比如, the C++ Standard Library contains the function template max(x, y) which returns the larger of x and y. That function template could be defined like this:

    template <typename T>
    inline T max(T a, T b) {
        return a > b ? a : b;
    }
    

    当我们调用这个 Template Function的时候,只要max(x1, x2)就可以了,编译器会自动选择对应的typename。

    函数模板/模板函数/Function template 可以 重载/overload,也就是说还可以定义与 函数模板同名的函数。

    例如:

    #include <iostream>
    #include <algorithm>
    
    template <typename T>
    inline T max(T a, T b) {
        return a > b ? a : b;
    }
    
    // 模板函数 重定义/overload/重载
    int max(int a, int b)
    {
    	return a>b ? a : b;
    }
    
    
    int main()
    {
    	std::cout << max(3,7) << std::endl;
    	std::cout << max(3.0, 7.0) << std::endl;
    	std::cout << max(3, 7.0) << std::endl;// 调用 重载/overloaded 的函数
    
    	std::cout << max<double>(3, 7.0) << std::endl;
    
    	return 0;
    }
    

    REFER: Function templates

    ②类模板/Class Template

    REFER: Class Template

    2、泛函编程/函数式编程/Functional Programming

    其,将电脑运算比作 数学 上的函数计算,并且避免使用程序状态以及易变物件。在 Functional Pgramming 中,最重要的基础是 λ演算/lambda calculus。

    纯粹的函数式编程语言: Haskell,Miranda,Concurrent Clean。
    非纯函数式编程语言:F#, Scala, Erlang, LISP, Mathematicar。

  • 相关阅读:
    UVA 1386 Cellular Automaton
    ZOJ 3331 Process the Tasks
    CodeForces 650B Image Preview
    CodeForces 650A Watchmen
    CodeForces 651B Beautiful Paintings
    CodeForces 651A Joysticks
    HUST 1601 Shepherd
    HUST 1602 Substring
    HUST 1600 Lucky Numbers
    POJ 3991 Seinfeld
  • 原文地址:https://www.cnblogs.com/xuanyuanchen/p/6039258.html
Copyright © 2011-2022 走看看