《C++捷径教程》读书笔记--Chapter 16--模板(完结)
来源:互联网 发布:非平衡面板和缺失数据 编辑:程序博客网 时间:2024/05/01 05:43
//--《C++捷径教程》读书笔记--Chapter 16--模板(完结)
//--Chapter 16--模板
//--04/16/2006 Sun.
//--Computer Lab
//--Liwei
//--程序#1 说明模板函数示例
#include <iostream>
using namespace std;
//template <class X> void swapargs(X &a, X &b)
template <typename X> 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: "<<i<<' '<<j<<'/n';
cout<<"Original x,y: "<<x<<' '<<y<<'/n';
cout<<"Original z,b: "<<a<<' '<<b<<'/n';
swapargs(i,j);
swapargs(x,y);
swapargs(a,b);
cout<<"/n===============/n";
cout<<"Swapped i,j: "<<i<<' '<<j<<'/n';
cout<<"Swapped x,y: "<<x<<' '<<y<<'/n';
cout<<"Swapped z,b: "<<a<<' '<<b<<'/n';
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#2 说明模板函数示例
#include <iostream>
using namespace std;
template <class type1, class type2>
void myfunc(type1 x, type2 y)
{
cout<<x<<' '<<y<<'/n';
}
int main()
{
myfunc(10, "hi00000000000000000000");
myfunc(0.23, 10L);
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#3 重载模板函数示例
#include <iostream>
using namespace std;
//template <class X> void swapargs(X &a, X &b)
template <typename X> void swapargs(X &a, X &b)
{
X temp;
temp=a;
a=b;
b=temp;
cout<<"Inside template swapargs./n";
}
template<> void swapargs<int>(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: "<<i<<' '<<j<<'/n';
cout<<"Original x,y: "<<x<<' '<<y<<'/n';
cout<<"Original z,b: "<<a<<' '<<b<<'/n';
swapargs(i,j);
swapargs(x,y);
swapargs(a,b);
cout<<"/n===============/n";
cout<<"Swapped i,j: "<<i<<' '<<j<<'/n';
cout<<"Swapped x,y: "<<x<<' '<<y<<'/n';
cout<<"Swapped z,b: "<<a<<' '<<b<<'/n';
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#4 重载模板本身
#include <iostream>
using namespace std;
template <class X> void f(X a)
{
cout<<"Inside f(X a)./n";
}
template <class X, class Y> 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 <iostream>
using namespace std;
template<class X>
void repeat(X data, int times)
{
do{
cout<<data<<'/n';
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 <iostream>
using namespace std;
template <class X>
X myabs(X val)
{
return val<0? -val: val;
}
int main()
{
cout<<myabs(-10)<<endl;
cout<<myabs(-10.0)<<endl;
cout<<myabs(-10L)<<endl;
cout<<myabs(-10.0F)<<endl;
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#7 通用类
#include <iostream>
using namespace std;
const int SIZE=100;
template <class QType>
class queue{
QType q[SIZE];
int sloc,rloc;
public:
queue() { sloc=rloc=0; }
void qput(QType i);
QType qget();
};
template <class QType>
void queue<QType>::qput(QType i)
{
if(sloc==SIZE)
{
cout<<"Queue is full./n";
return;
}
sloc++;
q[sloc]=i;
}
template <class QType>
QType queue<QType>::qget()
{
if(rloc==sloc)
{
cout<<"Queue Underflow./n";
return 0;
}
rloc++;
return q[rloc];
}
int main()
{
queue<int> a,b;
a.qput(10);
b.qput(19);
a.qput(20);
b.qput(1);
cout<<a.qget()<<" ";
cout<<a.qget()<<" ";
cout<<b.qget()<<" ";
cout<<b.qget()<<endl;
queue<double> c,d;
c.qput(10.12);
d.qput(19.99);
c.qput(-20.0);
d.qput(0.986);
cout<<c.qget()<<" ";
cout<<c.qget()<<" ";
cout<<d.qget()<<" ";
cout<<d.qget()<<endl;
/////////////////////////////////
queue<char *> s;
s.qput("liwei");
s.qput("tanfeng");
cout<<s.qget()<<" ";
cout<<s.qget()<<endl;
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#8 通用类
#include <iostream>
using namespace std;
template <class Type1, class Type2>
class myclass{
Type1 i;
Type2 j;
public:
myclass(Type1 a, Type2 b) { i=a; j=b; }
void show() { cout<<i<<' '<<j<<'/n'; }
};
int main()
{
myclass<int, double> ob1(10, 0.23);
myclass<char, char *> ob2('X', "This is a test.");
ob1.show();
ob2.show();
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#9 通用安全数组
#include <iostream>
using namespace std;
const int SIZE=10;
template <class AType>
class atype{
AType a[SIZE];
public:
atype() { register int i; for(i=0; i<SIZE; i++) a[i]=i; }
AType &operator[](int i);
};
template <class AType>
AType &atype<AType>::operator [](int i)
{
if(i<0 || i>SIZE-1)
{
cout<<"/nIndex value of "<<i<<" is out-of-bounds./n";
exit(1);
}
return a[i];
}
int main()
{
atype<int> intob;
atype<double> doubleob;
int i;
cout<<"Integer array: ";
for(i=0; i<SIZE; i++) intob[i]=i;
for(i=0; i<SIZE; i++) cout<<intob[i]<<" ";
cout<<endl;
cout<<"Double array: ";
for(i=0; i<SIZE; i++) doubleob[i]=(double)i/3;
for(i=0; i<SIZE; i++) cout<<doubleob[i]<<" ";
cout<<endl;
intob[12]=100;
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#10 通用安全数组
#include <iostream>
using namespace std;
//const int SIZE=10;
template <class AType, int size>
class atype{
AType a[size];
public:
atype() { register int i; for(i=0; i<size; i++) a[i]=i; }
AType &operator[](int i);
};
template <class AType, int size>
AType &atype<AType, size>::operator [](int i)
{
if(i<0 || i>size-1)
{
cout<<"/nIndex value of "<<i<<" is out-of-bounds./n";
exit(1);
}
return a[i];
}
int main()
{
atype<int,10> intob;
atype<double,15> doubleob;
int i;
cout<<"Integer array: ";
//for(i=0; i<10; i++) intob[i]=i;
for(i=0; i<10; i++) cout<<intob[i]<<" ";
cout<<endl;
cout<<"Double array: ";
//for(i=0; i<15; i++) doubleob[i]=(double)i/3;
for(i=0; i<15; i++) cout<<doubleob[i]<<" ";
cout<<endl;
intob[12]=100;
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#11 通用安全数组
#include <iostream>
using namespace std;
//const int SIZE=10;
template <class AType=int, int size=10>
class atype{
AType a[size];
public:
atype() { register int i; for(i=0; i<size; i++) a[i]=i; }
AType &operator[](int i);
};
template <class AType, int size>
AType &atype<AType, size>::operator [](int i)
{
if(i<0 || i>size-1)
{
cout<<"/nIndex value of "<<i<<" is out-of-bounds./n";
exit(1);
}
return a[i];
}
int main()
{
atype<int, 100> intarray;
atype<double> doublearray;
atype<> defarray;
int i;
cout<<"int array: ";
for(i=0; i<100; i++) intarray[i]=i;
for(i=0; i<100; i++) cout<<intarray[i]<<" ";
cout<<endl;
cout<<"double array: ";
for(i=0; i<10; i++) doublearray[i]=(double)i/3;
for(i=0; i<10; i++) cout<<doublearray[i]<<" ";
cout<<endl;
cout<<"defarray array: ";
for(i=0; i<10; i++) defarray[i]=i;
for(i=0; i<10; i++) cout<<defarray[i]<<" ";
cout<<endl;
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#12 通用类的显式特例化
#include <iostream>
using namespace std;
template <class T>
class myclass{
T x;
public:
myclass(T a) { cout<<"Inside generic myclass./n"; x=a; }
T getx() { return x;}
};
template<> class myclass<int>{
int x;
public:
myclass(int a) { cout<<"Inside myclass<int> specialization./n"; x=a*a; }
int getx() { return x;}
};
int main()
{
myclass<double> d(10.1);
cout<<"double: "<<d.getx()<<"/n/n";
myclass<int> i(5);
cout<<"int: "<<i.getx()<<endl;
return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
- 《C++捷径教程》读书笔记--Chapter 16--模板(完结)
- 《C++捷径教程》读书笔记--Chapter 11--类(完结)
- 《C++捷径教程》读书笔记--Chapter 14--继承(完结)
- 《C++捷径教程》读书笔记--Chapter 17--异常处理(完结)
- 《C++捷径教程》读书笔记--Chapter 12--类的深入学习(完结)
- 《C++捷径教程》读书笔记--Chapter 13--运算符的重载(完结)
- 《C++捷径教程》读书笔记--Chapter 15--虚函数与多态(完结)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第一部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第三部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第四部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第五部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第六部分)
- 《C++捷径教程》读书笔记--Chapter 18--C++的I/O系统-16
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第二部分)
- Java与.NET 的Web Services相互调用
- 一个人的奋斗历程
- web service 开发实战
- 把html的表格导入到excel,word中
- 用J2EE开发WebService
- 《C++捷径教程》读书笔记--Chapter 16--模板(完结)
- [摘录]CBD片语-001
- 用Web Services整合.NET和J2EE
- 什么时候应该使用Web Service
- 清华生7天猎头生活的发现——IT这行,有钱人真多(转)
- 用ASP.NET开发Web服务的五则技巧
- Databse Transaction
- Debug的使用
- 移动硬盘常见问题的解决