zoukankan      html  css  js  c++  java
  • C++中函数模板template和函数参数为指针,且有返回值的结合使用

     1 #include<iostream>
     2 using namespace std;
     3 // 利用模板函数计算一个表达式
     4 template<class Type>
     5 Type Abc(Type a,Type b,Type c)
     6 {
     7     return a+b+c;
     8 }
     9 // 利用引用参数指针计算一个表达式
    10 template<class Type>
    11 Type ABC(Type *a,Type *b,Type *c)
    12 {
    13     return (*a)+(*b)+(*c);
    14 
    15 }
    16 
    17 int main()
    18 {
    19     int a=1,b=2,c=3;
    20     cout<<"a= "<<a<<",b= "<<b<<",c= "<<c<<endl;
    21     cout<<"使用函数模板计算表达式的结果为:
    ";
    22     cout<<Abc(a,b,c)<<endl;
    23     float f=4,d=0.6,e=2.3;
    24     cout<<"f= "<<f<<",d= "<<d<<",e= "<<e<<endl;
    25     cout<<"使用函数模板计算表达式的结果为:
    ";
    26     cout<<Abc(f,d,e)<<endl;
    27     int *p2,*q2,*r2;
    28     p2=&a;
    29     q2=&b;
    30     r2=&c;
    31     
    32     cout<<"*p2= "<<*p2<<",*q2= "<<*q2<<",*r2= "<<*r2<<endl;
    33     cout<<"使用引用参数指针计算表达式的结果为:"<<endl;
    34     cout<<ABC(p2,q2,r2)<<endl;
    35     return 0;
    36 }

    程序中有两个模板函数,Type Abc(Type a,Type b,Type c),Type ABC(Type *a,Type *b,Type *c)两个的参数不同;

    代码调试运行结果为:

  • 相关阅读:
    HDU 1114 Piggy-Bank
    HDU 2955 Robberies
    NTOJ 290 动物统计(加强版)
    POJ 3624 Charm Bracelet
    HDU 2602 Bone Collector
    POJ 1523 SPF(无向图割顶)
    HDU 5311 Hidden String
    HDU 1421 搬寝室
    HDU 1058 Humble Numbers
    POJ 3259 Wormholes(spfa判负环)
  • 原文地址:https://www.cnblogs.com/yuzhuwei/p/4172280.html
Copyright © 2011-2022 走看看