77范文网 - 专业文章范例文档资料分享平台

第11章 模板(2)

来源:网络收集 时间:2019-04-13 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

swap(a,b); swap(x,y);

cout<(a) (b) (c) (d)

2,1 swap two int swap tow int swap tow int z,x 2,1 swap tow int swap tow int 2.2,1.1 z,x 2,1 swap tow int 2.2,1.1 z,x 2,1 2.2,1.1 z,x 2.2,1.1

5.设有函数模板 template T max(T a,T b) { return (a>b)?a:b; }

则下列语句中对该函数模板的错误使用是()。

(a)max(1,2) (b)max(2.3,4.5) (c)max(1,2.3) (d)max(‘a’,’b’)

6.关于在调用模板函数时模板实参的使用,下列描述正确的是()。

(a)对于虚拟类型参数所对应的模板实参,如果能从模板函数的实参中获得相同的信息,则都可以省略。

(b)对于虚拟类型参数所对应的模板实参,如果它们是参数表中的最后的若干个参数,则都可以省略。

(c)对于虚拟类型参数所对应的模板实参,若能够省略则必须省略。 (d)对于常规参数所对应的模板实参,任何情况下都不能省略。

二、填空题

1.模板是实现类属机制的一种工具,其功能非常强大,它既允许用户构造类属函数,即______________;也允许用户构造类属类,即____________。

2.有如下函数模板定义,错误的语句是_______,应该正为______________________。 template ;//① T fun(T x)//② { return x;//③ }

3.运行下列程序的输出结果为____________________。 #include template class Tstack { enum {size=1000}; T stack[size]; int top; public: Tstack( ):top(0){ } bool push(const T &i){ if(top

{ stack[top++]=i; return true; } else return false; } bool pop(T &value){ if(top!=0) { value=stack[--top] ; return true; } else return false; } };

void main() { Tstack a; int x; a.push(1); a.push(2); a.pop(x); cout<

4.下面的程序输出结果是____________________。 #include template T sum(T *x,int size=0) { T a=0; for(int i=0;i

void main() { int array[]={1,2,3,4}; int arraysum=sum(array,4); cout<

5.下面是一个函数模板,用于实现从3个整数或浮点数中找出最大值。请将函数模板的定义补充完整。 template

T max(_______①_______,T c) { T x; x=(a>b)?(a):(b); _______②_________; }

6.有如下程序段,错误的语句是_______,应该正为______________________。 template //①

class A { T x,y;//② public: A(T i,T j):x(i),y(j){}//③ };

A(double) obj(1.1,2.2);// ④

7.下面是3个数字求和的类模板程序。请将该定义补充完整。 template class sum { T temp,m[size]; public: sum(T a,T b,T c){m[0]=a;m[1]=b;m[2]=c;} T s(){return ________②________;} //计算三个数的和 };

三、编程题

编写一个处理冒泡排序的函数模板,并设计主程序完成输入、排序和输出int型和double型数组的功能。 【参考答案】 一、选择题 1. b 2. d 3. d 4. b 5. c 6. d

二、填空题

1. 函数模板、类模板

2. ①、template 即去掉分号 3. 21 4. 10

5. ①T a,T b 、 ②return (x>c)?(x):(c) 6. ④、A obj(1.1,2.2);

7. ①int size=3 、② m[0]+m[1]+m[2]

三、编程题 参考程序如下: #include

template void bubble_sort(X *items, int size); template void show_items(X *items, int size); void main(void) {

int iarray[7] = {7, 5, 4, 3, 9, 8, 6};

double darray[5] = {4.2, 2.5, -0.9, 100.2, 3.0}; cout << \ show_items(iarray, 7);

cout << \ show_items(darray, 5); bubble_sort(iarray, 7); bubble_sort(darray, 5);

cout << \ show_items(iarray, 7);

cout << \

show_items(darray, 5); }

template void bubble_sort(X *items, int size) {

register int i, j; X temp;

for (i = 1; i < size; i++) for (j = size-1; j >= i; j--) if (items[j-1] > items[j]) {

temp = items[j-1]; items[j-1] = items[j]; items[j] = temp; } }

template void show_items(X *items, int size) {

int i;

for(i=0; i < size; i++)

cout << items[i] << \ cout << endl; }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库第11章 模板(2)在线全文阅读。

第11章 模板(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/585725.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: