📄 chap18.lst
字号:
listing 1
// Function template example.
#include <iostream>
using namespace std;
// This is a function template.
template <class 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 a, b: " << a << ' ' << b << '\n';
swapargs(i, j); // swap integers
swapargs(x, y); // swap floats
swapargs(a, b); // swap chars
cout << "Swapped i, j: " << i << ' ' << j << '\n';
cout << "Swapped x, y: " << x << ' ' << y << '\n';
cout << "Swapped a, b: " << a << ' ' << b << '\n';
return 0;
}
listing 2
template <class X> void swapargs(X &a, X &b)
listing 3
template <class X>
void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
listing 4
// This will not compile.
template <class X>
int i; // this is an error
void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
listing 5
#include <iostream>
using namespace std;
template <class type1, class type2>
void myfunc(type1 x, type2 y)
{
cout << x << ' ' << y << '\n';
}
int main()
{
myfunc(10, "I like C++");
myfunc(98.6, 19L);
return 0;
}
listing 6
// Overriding a template function.
#include <iostream>
using namespace std;
template <class X> void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
cout << "Inside template swapargs.\n";
}
// This overrides the generic version of swapargs() for ints.
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 a, b: " << a << ' ' << b << '\n';
swapargs(i, j); // calls explicitly overloaded swapargs()
swapargs(x, y); // calls generic swapargs()
swapargs(a, b); // calls generic swapargs()
cout << "Swapped i, j: " << i << ' ' << j << '\n';
cout << "Swapped x, y: " << x << ' ' << y << '\n';
cout << "Swapped a, b: " << a << ' ' << b << '\n';
return 0;
}
listing 7
// Use new-style specialization syntax.
template<> void swapargs<int>(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << "Inside swapargs int specialization.\n";
}
listing 8
// Overload a function template declaration.
#include <iostream>
using namespace std;
// First version of f() template.
template <class X> void f(X a)
{
cout << "Inside f(X a)\n";
}
// Second version of f() template.
template <class X, class Y> void f(X a, Y b)
{
cout << "Inside f(X a, Y b)\n";
}
int main()
{
f(10); // calls f(X)
f(10, 20); // calls f(X, Y)
return 0;
}
listing 9
// Using standard parameters in a template function.
#include <iostream>
using namespace std;
const int TABWIDTH = 8;
// Display data at specified tab position.
template<class X> void tabOut(X data, int tab)
{
for(; tab; tab--)
for(int i=0; i<TABWIDTH; i++) cout << ' ';
cout << data << "\n";
}
int main()
{
tabOut("This is a test", 0);
tabOut(100, 1);
tabOut('X', 2);
tabOut(10/3, 3);
return 0;
}
listing 10
#include <iostream>
#include <cmath>
using namespace std;
void myfunc(int i)
{
cout << "value is: " << i << "\n";
}
void myfunc(double d)
{
double intpart;
double fracpart;
fracpart = modf(d, &intpart);
cout << "Fractional part: " << fracpart;
cout << "\n";
cout << "Integer part: " << intpart;
}
int main()
{
myfunc(1);
myfunc(12.2);
return 0;
}
listing 11
// A Generic bubble sort.
#include <iostream>
using namespace std;
template <class X> void bubble(
X *items, // pointer to array to be sorted
int count) // number of items in array
{
register int a, b;
X t;
for(a=1; a<count; a++)
for(b=count-1; b>=a; b--)
if(items[b-1] > items[b]) {
// exchange elements
t = items[b-1];
items[b-1] = items[b];
items[b] = t;
}
}
int main()
{
int iarray[7] = {7, 5, 4, 3, 9, 8, 6};
double darray[5] = {4.3, 2.5, -0.9, 100.2, 3.0};
int i;
cout << "Here is unsorted integer array: ";
for(i=0; i<7; i++)
cout << iarray[i] << ' ';
cout << endl;
cout << "Here is unsorted double array: ";
for(i=0; i<5; i++)
cout << darray[i] << ' ';
cout << endl;
bubble(iarray, 7);
bubble(darray, 5);
cout << "Here is sorted integer array: ";
for(i=0; i<7; i++)
cout << iarray[i] << ' ';
cout << endl;
cout << "Here is sorted double array: ";
for(i=0; i<5; i++)
cout << darray[i] << ' ';
cout << endl;
return 0;
}
listing 12
// A Generic array compaction function.
#include <iostream>
using namespace std;
template <class X> void compact(
X *items, // pointer to array to be compacted
int count, // number of items in array
int start, // starting index of compacted region
int end) // ending index of compacted region
{
register int i;
for(i=end+1; i<count; i++, start++)
items[start] = items[i];
/* For the sake of illustration, the remainder of
the array will be zeroed. */
for( ; start<count; start++) items[start] = (X) 0;
}
int main()
{
int nums[7] = {0, 1, 2, 3, 4, 5, 6};
char str[18] = "Generic Functions";
int i;
cout << "Here is uncompacted integer array: ";
for(i=0; i<7; i++)
cout << nums[i] << ' ';
cout << endl;
cout << "Here is uncompacted string: ";
for(i=0; i<18; i++)
cout << str[i] << ' ';
cout << endl;
compact(nums, 7, 2, 4);
compact(str, 18, 6, 10);
cout << "Here is compacted integer array: ";
for(i=0; i<7; i++)
cout << nums[i] << ' ';
cout << endl;
cout << "Here is compacted string: ";
for(i=0; i<18; i++)
cout << str[i] << ' ';
cout << endl;
return 0;
}
listing 13
// This function demonstrates a generic stack.
#include <iostream>
using namespace std;
const int SIZE = 10;
// Create a generic stack class
template <class StackType> class stack {
StackType stck[SIZE]; // holds the stack
int tos; // index of top-of-stack
public:
stack() { tos = 0; } // initialize stack
void push(StackType ob); // push object on stack
StackType pop(); // pop object from stack
};
// Push an object.
template <class StackType> void stack<StackType>::push(StackType ob)
{
if(tos==SIZE) {
cout << "Stack is full.\n";
return;
}
stck[tos] = ob;
tos++;
}
// Pop an object.
template <class StackType> StackType stack<StackType>::pop()
{
if(tos==0) {
cout << "Stack is empty.\n";
return 0; // return null on empty stack
}
tos--;
return stck[tos];
}
int main()
{
// Demonstrate character stacks.
stack<char> s1, s2; // create two character stacks
int i;
s1.push('a');
s2.push('x');
s1.push('b');
s2.push('y');
s1.push('c');
s2.push('z');
for(i=0; i<3; i++) cout << "Pop s1: " << s1.pop() << "\n";
for(i=0; i<3; i++) cout << "Pop s2: " << s2.pop() << "\n";
// demonstrate double stacks
stack<double> ds1, ds2; // create two double stacks
ds1.push(1.1);
ds2.push(2.2);
ds1.push(3.3);
ds2.push(4.4);
ds1.push(5.5);
ds2.push(6.6);
for(i=0; i<3; i++) cout << "Pop ds1: " << ds1.pop() << "\n";
for(i=0; i<3; i++) cout << "Pop ds2: " << ds2.pop() << "\n";
return 0;
}
listing 14
stack<char> s1, s2; // create two character stacks
stack<double> ds1, ds2; // create two double stacks
listing 15
stack<char *> chrptrQ;
listing 16
struct addr {
char name[40];
char street[40];
char city[30];
char state[3];
char zip[12];
};
listing 17
stack<addr> obj;
listing 18
/* This example uses two generic data types in a
class definition.
*/
#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', "Templates add power.");
ob1.show(); // show int, double
ob2.show(); // show char, char *
return 0;
}
listing 19
// A generic safe array example.
#include <iostream>
#include <cstdlib>
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);
};
// Provide range checking for atype.
template <class AType> AType &atype<AType>::operator[](int i)
{
if(i<0 || i> SIZE-1) {
cout << "\nIndex value of ";
cout << i << " is out-of-bounds.\n";
exit(1);
}
return a[i];
}
int main()
{
atype<int> intob; // integer array
atype<double> doubleob; // double array
int i;
cout << "Integer array: ";
for(i=0; i<SIZE; i++) intob[i] = i;
for(i=0; i<SIZE; i++) cout << intob[i] << " ";
cout << '\n';
cout << "Double array: ";
for(i=0; i<SIZE; i++) doubleob[i] = (double) i/3;
for(i=0; i<SIZE; i++) cout << doubleob[i] << " ";
cout << '\n';
intob[12] = 100; // generates runtime error
return 0;
}
listing 20
// Demonstrate non-type template arguments.
#include <iostream>
#include <cstdlib>
using namespace std;
// Here, int size is a non-type agument.
template <class AType, int size> class atype {
AType a[size]; // length of array is passed in size
public:
atype() {
register int i;
for(i=0; i<size; i++) a[i] = i;
}
AType &operator[](int i);
};
// Provide range checking for atype.
template <class AType, int size>
AType &atype<AType, size>::operator[](int i)
{
if(i<0 || i> size-1) {
cout << "\nIndex value of ";
cout << i << " is out-of-bounds.\n";
exit(1);
}
return a[i];
}
int main()
{
atype<int, 10> intob; // integer array of size 10
atype<double, 15> doubleob; // double array of size 15
int i;
cout << "Integer array: ";
for(i=0; i<10; i++) intob[i] = i;
for(i=0; i<10; i++) cout << intob[i] << " ";
cout << '\n';
cout << "Double array: ";
for(i=0; i<15; i++) doubleob[i] = (double) i/3;
for(i=0; i<15; i++) cout << doubleob[i] << " ";
cout << '\n';
intob[12] = 100; // generates runtime error
return 0;
}
listing 21
size = 10; // Error
listing 22
template <class X=int> class myclass { //...
listing 23
// Demonstrate default template arguments.
#include <iostream>
#include <cstdlib>
using namespace std;
// Here, AType defaults to int and size defaults to 10.
template <class AType=int, int size=10> class atype {
AType a[size]; // size of array is passed in size
public:
atype() {
register int i;
for(i=0; i<size; i++) a[i] = i;
}
AType &operator[](int i);
};
// Provide range checking for atype.
template <class AType, int size>
AType &atype<AType, size>::operator[](int i)
{
if(i<0 || i> size-1) {
cout << "\nIndex value of ";
cout << i << " is out-of-bounds.\n";
exit(1);
}
return a[i];
}
int main()
{
atype<int, 100> intarray; // integer array, size 100
atype<double> doublearray; // double array, default size
atype<> defarray; // default to int array of size 10
int i;
cout << "int array: ";
for(i=0; i<100; i++) intarray[i] = i;
for(i=0; i<100; i++) cout << intarray[i] << " ";
cout << '\n';
cout << "double array: ";
for(i=0; i<10; i++) doublearray[i] = (double) i/3;
for(i=0; i<10; i++) cout << doublearray[i] << " ";
cout << '\n';
cout << "defarray array: ";
for(i=0; i<10; i++) defarray[i] = i;
for(i=0; i<10; i++) cout << defarray[i] << " ";
cout << '\n';
return 0;
}
listing 24
template <class AType=int, int size=10> class atype {
listing 25
// Demonstrate class specialization.
#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; }
};
// Explicit specialization for int.
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() << "\n";
return 0;
}
listing 26
template <> class myclass<int> {
listing 27
template <typename X> void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
listing 28
typename X::Name someObject;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -