template

Templates能用来创建一个对未知数据类型的操作的函数模板.这个通过用其它数据类型代替一个占位符data-type 来实现. 例如:

    template<class X> void genericSwap( X &a, X &b ) {
      X tmp;
  
      tmp = a;
      a = b;
      b = tmp;
    }     

    int main(void) {

      ...

      int num1 = 5;
      int num2 = 21;

      cout << "Before, num1 is " << num1 << " and num2 is " << num2 << endl;
      genericSwap( num1, num2 );
      cout << "After, num1 is " << num1 << " and num2 is " << num2 << endl;

      char c1 = 'a';
      char c2 = 'z';

      cout << "Before, c1 is " << c1 << " and c2 is " << c2 << endl;
      genericSwap( c1, c2 );
      cout << "After, c1 is " << c1 << " and c2 is " << c2 << endl;

      ...

      return( 0 );
    }
posted @   存在与虚无  阅读(243)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示