热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

《C++捷径教程》读书笔记--Chapter16--模板(完结)

--《C++捷径教程》读书笔记--Chapter16--模板(完结)--Chapter16--模板--04162006Sun.--ComputerLab--L

//--《C++捷径教程》读书笔记--Chapter 16--模板(完结)
//--Chapter 16--模板
//--04/16/2006 Sun.
//--Computer Lab
//--Liwei

 

//--程序#1  说明模板函数示例
#include
using namespace std;

//template void swapargs(X &a, X &b)
template void swapargs(X &a, X &b)
{
 X temp;
 temp=a;
 a=b;
 b=temp;
}

int main()
{
 int i=10, j=20;
 double x=10.1, y=23.3;
 char a='x', b='z';

 cout<<"Original i,j: "< cout<<"Original x,y: "< cout<<"Original z,b: "<

 swapargs(i,j);
 swapargs(x,y);
 swapargs(a,b);

 cout<<"/n===============/n";

 cout<<"Swapped i,j: "< cout<<"Swapped x,y: "< cout<<"Swapped z,b: "<

 return 0;
}

//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#2  说明模板函数示例
#include
using namespace std;

template
void myfunc(type1 x, type2 y)
{
 cout<}

int main()
{
 myfunc(10, "hi00000000000000000000");
 myfunc(0.23, 10L);

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#3  重载模板函数示例
#include
using namespace std;

//template void swapargs(X &a, X &b)
template void swapargs(X &a, X &b)
{
 X temp;
 temp=a;
 a=b;
 b=temp;
 cout<<"Inside template swapargs./n";
}

template<> void swapargs(int &a, int &b)
//void swapargs(int &a, int &b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;
 cout<<"Inside swapargs int specialization./n";
}

int main()
{
 int i=10, j=20;
 double x=10.1, y=23.3;
 char a='x', b='z';

 cout<<"Original i,j: "< cout<<"Original x,y: "< cout<<"Original z,b: "<

 swapargs(i,j);
 swapargs(x,y);
 swapargs(a,b);

 cout<<"/n===============/n";

 cout<<"Swapped i,j: "< cout<<"Swapped x,y: "< cout<<"Swapped z,b: "<

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#4  重载模板本身
#include
using namespace std;

template void f(X a)
{
 cout<<"Inside f(X a)./n";
}

template void f(X a, Y b)
{
 cout<<"Inside f(X a, Y b)./n";
}

int main()
{
 f(10);
 f("dddddddd",544);

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#5  标准类型参数
#include
using namespace std;

template
void repeat(X data, int times)
{
 do{
  cout<  times--;
 }while(times);
}

int main()
{
 repeat("This is a test.", 311);
 repeat(100, 5);
 repeat(99.0/2, 4);

 return 0;
}

//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#6  通用函数myabs()
#include
using namespace std;

template
X myabs(X val)
{
 return val<0? -val: val;
}

int main()
{
 cout< cout< cout< cout<

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#7  通用类
#include
using namespace std;

const int SIZE=100;

template
class queue{
 QType q[SIZE];
 int sloc,rloc;
public:
 queue() { sloc=rloc=0; }
 void qput(QType i);
 QType qget();
};

template
void queue::qput(QType i)
{
 if(sloc==SIZE)
 {
  cout<<"Queue is full./n";
  return;
 }
 sloc++;
 q[sloc]=i;
}

template
QType queue::qget()
{
 if(rloc==sloc)
 {
  cout<<"Queue Underflow./n";
  return 0;
 }
 rloc++;
 return q[rloc];
}

int main()
{
 queue a,b;

    a.qput(10);
 b.qput(19);
 a.qput(20);
 b.qput(1);

 cout< cout< cout< cout<

 queue c,d;

 c.qput(10.12);
 d.qput(19.99);
 c.qput(-20.0);
 d.qput(0.986);

 cout< cout< cout< cout<

/////////////////////////////////
 queue s;
 s.qput("liwei");
 s.qput("tanfeng");


 cout< cout< 
 return 0;

}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#8  通用类
#include
using namespace std;

template
class myclass{
 Type1 i;
 Type2 j;
public:
 myclass(Type1 a, Type2 b) { i=a; j=b; }
 void show() { cout<};

int main()
{
 myclass ob1(10, 0.23);
 myclass ob2('X', "This is a test.");

 ob1.show();
 ob2.show();

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#9  通用安全数组
#include
using namespace std;

const int SIZE=10;

template
class atype{
 AType  a[SIZE];
public:
 atype() { register int i;  for(i=0; i AType  &operator[](int i);
};

template
AType &atype::operator [](int i)
{
 if(i<0 || i>SIZE-1)
 {
  cout<<"/nIndex value of "<  exit(1);
 }

 return a[i];
}

int main()
{
 atype intob;
 atype doubleob;

 int i;

 cout<<"Integer array: ";
 for(i=0; i for(i=0; i cout<

 cout<<"Double array: ";
 for(i=0; i for(i=0; i cout<

 intob[12]=100;

 return 0;
}

//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#10  通用安全数组
#include
using namespace std;

//const int SIZE=10;

template
class atype{
 AType  a[size];
public:
 atype() { register int i;  for(i=0; i AType  &operator[](int i);
};

template
AType &atype::operator [](int i)
{
 if(i<0 || i>size-1)
 {
  cout<<"/nIndex value of "<  exit(1);
 }

 return a[i];
}

int main()
{
 atype intob;
 atype doubleob;

 int i;

 cout<<"Integer array: ";
 //for(i=0; i<10; i++) intob[i]=i;
 for(i=0; i<10; i++) cout< cout<

 cout<<"Double array: ";
 //for(i=0; i<15; i++) doubleob[i]=(double)i/3;
 for(i=0; i<15; i++) cout< cout<

 intob[12]=100;

 return 0;
}

//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#11  通用安全数组
#include
using namespace std;

//const int SIZE=10;

template
class atype{
 AType  a[size];
public:
 atype() { register int i;  for(i=0; i AType  &operator[](int i);
};

template
AType &atype::operator [](int i)
{
 if(i<0 || i>size-1)
 {
  cout<<"/nIndex value of "<  exit(1);
 }

 return a[i];
}

int main()
{
 atype  intarray;
 atype doublearray;
 atype<> defarray;

 int i;

 cout<<"int array: ";
 for(i=0; i<100; i++) intarray[i]=i;
 for(i=0; i<100; i++) cout< cout<

 cout<<"double array: ";
 for(i=0; i<10; i++) doublearray[i]=(double)i/3;
 for(i=0; i<10; i++) cout< cout<

 cout<<"defarray array: ";
 for(i=0; i<10; i++) defarray[i]=i;
 for(i=0; i<10; i++) cout< cout<

 return 0;
}

//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#12  通用类的显式特例化
#include
using namespace std;

template
class myclass{
 T x;
public:
 myclass(T a) { cout<<"Inside generic myclass./n"; x=a; }
 T getx() { return x;}
};

template<> class myclass{
 int x;
public:
 myclass(int a) { cout<<"Inside myclass specialization./n"; x=a*a; }
 int getx() { return x;}
};


int main()
{
 myclass  d(10.1);
 cout<<"double: "<

 myclass  i(5);
 cout<<"int: "<

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//


推荐阅读
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 结构体在内存中的对齐规则
    一个结构体变量定义完之后,其在内存中的存储并不等于其所包含元素的宽度之和。例一:#include<iostream ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
author-avatar
天之道
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有