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
则下列语句中对该函数模板的错误使用是()。
(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
3.运行下列程序的输出结果为____________________。 #include { 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 4.下面的程序输出结果是____________________。 #include 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 三、编程题 编写一个处理冒泡排序的函数模板,并设计主程序完成输入、排序和输出int型和double型数组的功能。 【参考答案】 一、选择题 1. b 2. d 3. d 4. b 5. c 6. d 二、填空题 1. 函数模板、类模板 2. ①、template 5. ①T a,T b 、 ②return (x>c)?(x):(c) 6. ④、A 7. ①int size=3 、② m[0]+m[1]+m[2] 三、编程题 参考程序如下: #include template 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 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 int i; for(i=0; i < size; i++) cout << items[i] << \ cout << endl; } 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库第11章 模板(2)在线全文阅读。
相关推荐: