2C语法筒基础1.pptVIP

  • 1
  • 0
  • 约8.7千字
  • 约 48页
  • 2018-12-22 发布于福建
  • 举报
2C语法筒基础1

参数泛化的swap() //如果把各种数据类型抽象为参数Type //并保证swap能接受各种已定义类型的参数 void swap(Type a, Type b) { Type temp; temp = a; a = b; b = temp; } //泛化以后的swap具有更强的通用性 int string complex complex 其他类型 认识模板 #include iostream #include string #include complex using namespace std; //这里的Type也可换成其他你愿意的,如class Any template class Type void Swap(Type a, Type b)//const能否加上? { Type temp; temp = a; a = b; b = temp; } int main() { int a1 = 5; int a2 = 3; cout before swap,integer1 = a1 ,integer2 = a2 endl; Swap(a1,a2);//int类型函数调用 cout after swap,integer1 = a1 ,integer2 = a2 endl;

文档评论(0)

1亿VIP精品文档

相关文档