• c++ template笔记(1)模板函数


    1.定义函数模板

    template <typename T>
    inline T const& max (T const& a, T const& b)
    {
        // if a < b then use b else use a
        return  a < b ? b : a;
    }
    

    2.使用模板函数

    #include <iostream>
    #include <string>
    #include "max.hpp"
    
    int main()
    {
        int i = 42;
        std::cout << "max(7,i):   " << ::max(7,i) << std::endl;
    
        double f1 = 3.4;
        double f2 = -6.7;
        std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;
    
        std::string s1 = "mathematics";
        std::string s2 = "math";
        std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
    }
    

    输出结果

    3.确定返回的参数

    ::max<double>(1,2); //ok
    //::max(1,1.2);//wrong
    ::max(static_cast<double>(4),4.1);//ok
    

       若两个参数不正确,或者不支持模板定义的特性,编译时则会出错

    4.多个模板参数

    template <typename T1,typename T2>
    inline T1 const& max (T1 const& a, T2 const& b)
    {
        // if a < b then use b else use a
        return  a < b ? b : a;
    }
    

    示例

    double i = 42.1;
    std::cout << "max(7,i):   " << ::max(7,i) << std::endl;
    

    返回值是T1,所以返回是int类型,结果是42,出错了

    定义3个参数,第3个参数用于表示返回值类型

    template <typename T1,typename T2,typename T3>
    inline T3 const& max (T1 const& a, T2 const& b)
    {
        // if a < b then use b else use a
        return  a < b ? b : a;
    }
    

    测试

    double i = 42.1;
    std::cout << "max(7,i):   " << ::max<int,double,double>(7,i) << std::endl;
    

    返回正确的42.1

    5.模板函数重载

      重载函数的最佳匹配遵循以下优先规则:
      a、完全匹配时,普通函数优于模板函数及模板函数的特化版本
      b、提升转换(如:char和short转换为int,及float转换为double)
      c、标准转换(如:int转换为char,及long转换为double)
      d、用户定义的转换,如类声明中定义的转换。
      加引用和const限定符之类的,属于无关紧要的转换,都可以认为是完全匹配,但是const优先级应该是高于引用的,编译器会优先尝试转换为const。


    // maximum of two int values inline int const& max (int const& a, int const& b) { return a < b ? b : a; } // maximum of two values of any type template <typename T> inline T const& max (T const& a, T const& b) { return a < b ? b : a; } // maximum of three values of any type template <typename T> inline T const& max (T const& a, T const& b, T const& c) { return ::max (::max(a,b), c); } int main() { ::max(7, 42, 68); // calls the template for three arguments ::max(7.0, 42.0); // calls max<double> (by argument deduction) ::max('a', 'b'); // calls max<char> (by argument deduction) ::max(7, 42); // calls the nontemplate for two ints ::max<>(7, 42); // calls max<int> (by argument deduction) ::max<double>(7, 42); // calls max<double> (no argument deduction) ::max('a', 42.7); // calls the nontemplate for two ints }

    特别注意::max<>(7, 42);这句的写法指定了需要调用模板的实例化。 
    注意:自动类型转换

    ::max('a', 42.7); 只适用于常规函数,'a’会完成自动类型转换

    6.字符串重载的例子

    #include <iostream>
    #include <cstring>
    #include <string>
    
    // maximum of two values of any type
    template <typename T>
    inline T const& max (T const& a, T const& b)
    {
        return  a < b  ?  b : a;
    }
    
    // maximum of two pointers
    template <typename T>
    inline T* const& max (T* const& a, T* const& b)
    {
        return  *a < *b  ?  b : a;
    }
    
    // maximum of two C-strings
    inline char const* const& max (char const* const& a,
                                   char const* const& b)
    { 
        return  std::strcmp(a,b) < 0  ?  b : a;
    }
    
    int main ()
    {
        int a=7;
        int b=42;
        ::max(a,b);      // max() for two values of type int
    
        std::string s="hey";
        std::string t="you";
        ::max(s,t);      // max() for two values of type std::string
    
        int* p1 = &b;
        int* p2 = &a;
        ::max(p1,p2);    // max() for two pointers
    
        char const* s1 = "David";
        char const* s2 = "Nico";
        ::max(s1,s2);    // max() for two C-strings
    }
    

    注意每个重载函数必须存在着一定的差异.

    7.总是要把重载函数定义在调用之前

    // maximum of two values of any type
    template <typename T>
    inline T const& max (T const& a, T const& b)
    {
        return  a < b ? b : a;
    }
    
    // maximum of three values of any type
    template <typename T>
    inline T const& max (T const& a, T const& b, T const& c)
    {
        return max (max(a,b), c);  // uses the template version even for ints
    }                              // because the following declaration comes
                                   // too late:
    // maximum of two int values
    inline int const& max (int const& a, int const& b) 
    {
        return  a < b ? b : a;
    }
    

    3参数的max调用的是2参数模板函数,而非最后定义的max非模板函数

    8.std:string和char

    #include <string>
    
    // note: reference parameters
    template <typename T>
    inline T const& max (T const& a, T const& b)
    {
        return  a < b  ?  b : a;
    }
    
    int main()
    {
        std::string s;
    
        ::max("apple","peach");   // OK: same type
        ::max("apple","tomato");  // ERROR: different types
        ::max("apple",s);         // ERROR: different types
    }
    

    “apple”会被转成 char[6]数组,所以比较要求数组维度长度相同,第二条”tomato”与”apple”长度不符合,即出错.

    s是std:: string类型,而非char数组,所以也出错

    9.以非引用参数方式传递

    #include <string>
    
    // note: nonreference parameters
    template <typename T>
    inline T max (T a, T b)
    {
        return  a < b  ?  b : a;
    }
    int main()
    {
        std::string s;
    
        ::max("apple","peach");   // OK: same type
        ::max("apple","tomato");  // OK: decays to same type
        ::max("apple",s);         // ERROR: different types
    }
    

    只有最后一个类型不符合的编译不通过

    转自:http://www.cnblogs.com/Clingingboy/archive/2011/03/08/1977140.html

  • 相关阅读:
    醉上清新年中工作总结汇报模板免费下载_PPTX图片设计素材_包图网888pic.com
    清爽通用工作总结汇报ppt模板免费下载_PPTX图片设计素材_包图网888pic.com
    工作年终总结通用计划规划商务述职年会模板
    清新时尚岗位竞聘个人介绍PPT模板(8598)_竞聘简历PPT_素材在线
    横圆版简约清新通用工作述职报告PPT模板(8456)_其他商务PPT_素材在线
    2016年终总结工作汇报PPT模板
    绿色简约工作汇报计划总结PPT- 模板下载页_变色龙
    工作报告模板下载_免费工作报告图片设计素材_第2页_包图网
    Netflix新放出来的开源工具Chaos Monkey
    微喜帖,微信喜帖,电子喜帖,电子请柬
  • 原文地址:https://www.cnblogs.com/dongzhiquan/p/2079303.html
Copyright © 2020-2023  润新知