Template习题

//
//因为这次公司培训机制改变为先发布题目,由学员看书做题,再由教员专向辅导。
//因此这次没有准备讲义,只把题目贴上来,朋友们也可以练练手,过几天把部分答案上传。
//本套题分为四个难度:BASE, ADVANCED, EXPLORATION, FUN. 思考难度指数分别大概是
//2,5,7,9(满分10)。其中BASE和ADVANCED是要求学员一定要做的。EXPLORATION
//是探索题目,FUN是探索题目做完后还有余力的一些很有意思有嚼头的题目。
//
//PS:自我感觉题目总体难度有些高,不知道学员能不能坚持下来...


//这里想要放一个目录链接到一下各个section的,但是不知道怎么做,有朋友知道的能说说吗?

BASE:
1. write the output.

None.gif template < class  Type >
None.gif
void  Func(Type  & x , Type  & y)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Type a
=x;
InBlock.gif    x
=y;
InBlock.gif    y
=a;
ExpandedBlockEnd.gif}

None.gif
None.gif#include 
< iostream >
None.gif
using   namespace  std;
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int a = 12;
InBlock.gif    
int b = 24;
InBlock.gif    Func(a,b);
InBlock.gif    cout
<<"a:"<<a<<" "<<"b:"<<b<<endl;
ExpandedBlockEnd.gif}

None.gif
None.gif

2. With the function Template:

None.gif template < int  size ,  class  T  >
None.gif
void  Show(T  * d)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
forint j=0;j<size;j++
InBlock.gif        cout
<<d[j]<<' ';
ExpandedBlockEnd.gif}

None.gif
None.gif

What is the template function prototype invoked by Show<10>("abcdefghijklmn")? What's the output?

3. the program's output is not right, please correct the following program with right semantics:

 

None.gif template < class  T >
None.gifT maxEx(T a,T b)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
return (a > b) ? a : b;
ExpandedBlockEnd.gif}

None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
int nFirst = 10;
InBlock.gif 
int nSecond = 16;
InBlock.gif cout
<<maxEx(nFirst, nSecond)<<endl;            //should be 16
InBlock.gif

InBlock.gif    
char *c1 = "a";
InBlock.gif    
char *c2 = "zoo";
InBlock.gif    
char *c3 = "ZOO";
InBlock.gif    
char *c4 = "A";
InBlock.gif    cout
<<maxEx(c1,c2)<<endl;                       //should be zoo
InBlock.gif
    cout<<maxEx(c3,c4)<<endl;                       //should be ZOO
InBlock.gif

InBlock.gif 
return 0;
ExpandedBlockEnd.gif}
 
None.gif
None.gif

4.write the output of the following code.

 

None.gif template  < class  T >   void  f(T)                     //  (d)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    cout
<<"d"<<endl;
ExpandedBlockEnd.gif}

None.giftemplate 
< class  T >   void  f( int , T,  double )        //  (e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    cout
<<"e"<<endl;
ExpandedBlockEnd.gif}

None.giftemplate 
< class  T >   void  f(T * )                    //  (f)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    cout
<<"f"<<endl;
ExpandedBlockEnd.gif}

None.giftemplate 
<>  
None.gif
void  f < double >  ( double )                          //  (g)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    cout
<<"g"<<endl;
ExpandedBlockEnd.gif}

None.gif
void  f( double )                                   //  (h)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    cout
<<"h"<<endl;
ExpandedBlockEnd.gif}

None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
bool b = true;
InBlock.gif    
int i = 1;
InBlock.gif    
double d = 3.14;
InBlock.gif    f(b);                   
InBlock.gif    f(i,
42,d) ;             
InBlock.gif    f(
&i) ;                 
InBlock.gif    f(d);                   
InBlock.gif
InBlock.gif 
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif

5.Case:

None.gif template  < class  Type >  
None.gif
class  TwoNum
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
private:
InBlock.gif    Type a;
InBlock.gif    Type b;
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif    TwoNum()
dot.gif{}
InBlock.gif    TwoNum(Type aa , Type bb);
InBlock.gif
InBlock.gif    
int Compare(); //比较a 和b的大小
InBlock.gif

InBlock.gif    Type Max() 
// 求a 和b的最大值
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
return (a>b)?a:b; 
ExpandedSubBlockEnd.gif    }

InBlock.gif    Type Min() 
// 求a 和b的最小值
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
return (a>b)?b:a;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void Setab(Type& aa , Type& bb)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        a
=aa; b=bb; 
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    Type geta()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return a;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    Type getb()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
return b;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
None.giftemplate
< class  Type >
None.gifTwoNum
< Type > ::TwoNum(Type aa ,Type bb)
None.gif                   : a(aa), b(bb)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    ;
ExpandedBlockEnd.gif}

None.gif
None.giftemplate
< class  Type >
None.gif
int  TwoNum < Type > ::Compare()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(a>b)
InBlock.gif        
return 1;
InBlock.gif    
else if(a==b)
InBlock.gif        
return 0;
InBlock.gif    
else
InBlock.gif        
return -1;
ExpandedBlockEnd.gif}

None.gif    
None.gif#include 
< iostream.h >
None.gif
void  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    TwoNum
<int> x(4,8);
InBlock.gif    cout
<<x.Compare()<<’ ‘<<x.Max()<<’ ‘<<x.Min()<<endl;
InBlock.gif
InBlock.gif    
char ch=’x’;
InBlock.gif    TwoNum
<char> y(ch,’x’);
InBlock.gif    cout
<<y.Compare()<<’ ‘<<y.Max()<<’ ‘<<y.Min()<<endl;
ExpandedBlockEnd.gif}

Please write the output:

6.Which of the following templates are illegal? Why?

(1)

None.gif template  < class  Type >
None.gif
class  Container1;
None.giftemplate 
< class  Type,  int  size >
None.gif
class  Container1;
None.gif
None.giftemplate 
< class  Type >
None.gif
void  Container1(Type t);
None.giftemplate 
< class  Type,  int  size >
None.gif
void  Container1(Type t);

(2)

None.gif template  < class  T,U, class  V >
None.gif
class  Container2;

(3)

None.gif template < typename myT,  class  myT >
None.gif
class  Container3;

(4)

None.gif template < class  Type,  int   * ptr)
None.gif
class  Container4;
None.giftemplate
< class  Type,  int   *  pi)
None.gif
class  Container4;

(5)

None.gif template  < class  Type,  int  val  =   0 >
None.gif
class  Container6;

7.correct the class CList declaration (No need to implement the members.)

 

None.gif template  < class  elementType >
None.gif
class  ListItem;
None.gif
None.giftemplate
< class  elemType >
None.gif
class  CList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    CList(): front(NULL), end(NULL);
InBlock.gif    CList (
const List &);
InBlock.gif    
~CList();
InBlock.gif
InBlock.gif    
void insert(ListItem<elemType> *ptr, elemType<elemType> value);
InBlock.gif    
int remove(elemType<elemType> value);
InBlock.gif    size_t size( )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return _size; 
ExpandedSubBlockEnd.gif    }

InBlock.gif
private:
InBlock.gif    ListItem
<elemType> *front;
InBlock.gif    ListItem
<elemType> *end;
ExpandedBlockEnd.gif}
;

8.write the output for overload class template

 

None.gif template  < class  T >   class  mvector                 //  (a)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    
void f()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout
<<"a"<<endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.giftemplate 
< class  T >   class  mvector < T *>              //  (b)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    T t;
InBlock.gif
public:
InBlock.gif    
void f()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        t 
= 0;
InBlock.gif        cout
<<"b"<<endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;  
None.giftemplate 
<>     class  mvector  < void *>               //  (c)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    
void f()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout
<<"c"<<endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    mvector
<void*>  vpm;
InBlock.gif    mvector
<int*>   ipm;
InBlock.gif    mvector
<int>    im;
InBlock.gif
InBlock.gif    vpm.f();
InBlock.gif    ipm.f();
InBlock.gif    im.f();
ExpandedBlockEnd.gif}



ADVANCED:
1.Write a class template CVar<typename> which has following methods:

None.gif      Operator  +  (CVar < typename >&  )   // plus
None.gif
     Operator –(CVar < typename >&  )   // division
None.gif
     Operator  = (CVar < typename >&  )   // assign
None.gif
     Operator  == (CVar < typename >&  )   // equal
None.gif
     Operator typename()         // cast
None.gif
     Output();        // output it’s data;
None.gif

And other necessary members.

2. Write some global function template:

None.gif      Operator  + ( CVar < Typename >&  , CVar < Typename >& )
None.gif     Operator 
- ( CVar < Typename >&  , CVar < Typename >& )

You can try to comment CVar class operator +/- to test whether your global function is enable.

3. Write a specialized template class CVar<char*> and CVar<bool>.
//Note: Using your semantics to implement the algorithm.
// such as :            CVar<char*1> + CVar<char*2> : strcat();
//                          CVar<char*1> - CVar<char*2> : find all char*2 and delete them in char*1.
//                          CVar<bool>   + CVar<bool>   : &
//                          CVar<bool>   - CVar<bool>   : ^

4. Write a template function my_sort as following:

None.gif  template < typename Type >
None.gif    Void my_sort(Type
*  array, size_t size);

//NOTE:
//* Maybe you have to implement another 2 function templates:

None.gif      template < typename Type >
None.gif     
bool  my_max(Type &  lhs, Type &  rhs);
None.gif     template
< typename Type >
None.gif     Void my_swap(Type
&  lhs, Type &  rhs);

//* you can decide your concrete sort algorithm.

EXPLORATION:
1. Design a template queue class and implement the following members:

(1) method void add(Type elem).     //add an element to tail.
(2) method Type remove().           //remove element from head, and return the element.
(3) method Type head().             //return the head value of queue.
(4) method void clear().            //clear all queue.
(5) method bool is_empty().         //indicate whether the queue is empty.
(6) method size_t count().          //return the count.
(7) nested class Iterator to traverse the queue.
(8) method operator+=(Type elem).    //The same to add();
(9) method operator--().             //The same to remove();
(10) method Iterator begin().        //Return the begin of the queue.
(11) method Iterator end().          //Return the end of the queue.

Besides, define an global overload operator:
    operator<<;
to directly output all the values of queue item.

the following is the test of your queue:

None.gif #include  < iostream.h >
None.gif#include 
" queue.h "
None.gif
using   namespace  std;
None.gif
None.gif
void  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Queue
<int> iq(10);
InBlock.gif    
InBlock.gif    
for(int i=0; i < 10; i++)
InBlock.gif        iq.add(i
*5);
InBlock.gif    iq
--;
InBlock.gif    iq 
+= 50;
InBlock.gif    
InBlock.gif    
if (!iq.is_empty())
InBlock.gif        cout 
<< "Removed : " << iq.remove() << " Current Head : " << iq.head();
InBlock.gif    cout 
<< endl;
InBlock.gif    
if (!iq.is_empty())
InBlock.gif        cout 
<< iq;
InBlock.gif    Queue
<int>::Iterator iQueue;
InBlock.gif    
for(iQueue = iq.begin(); iQueue != iq.end(); iQueue++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< (*iQueue)<<" ";
ExpandedSubBlockEnd.gif    }

InBlock.gif    cout 
<< endl;
InBlock.gif    iq.clear();
InBlock.gif    cout 
<< iq;
ExpandedBlockEnd.gif}

The ourput is:
Removed : 5 Current Head : 10
There are 9 queue items: < 10 15 20 25 30 35 40 45 50 >
10 15 20 25 30 35 40 45 50
There are 0 queue items: < >

2.design a class template CHash which algorithm is besed on buckets:
template< typename Key, typename Value >

None.gif class  CHash
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    
class Iterator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    CHash( 
const size_t nMax,
InBlock.gif           ULONG        ( 
*hashFunc )( const Key tKey ),
InBlock.gif           
bool         bAutoSize = false );
InBlock.gif CHash( 
const CHash& hash );
InBlock.gif 
~CHash();
InBlock.gif CHash
& operator= ( const CHash<Key, Value>& hash );
InBlock.gif    
operator[];
InBlock.gif    Iterator begin();
InBlock.gif    Iterator end();
InBlock.gif size_t size();
InBlock.gif 
void insert( const Key tKey, Value& rValue );
InBlock.gif 
void clear();
ExpandedBlockEnd.gif}


FUN:

1.Write the output of the following code.

None.gif #include  < IOSTREAM >
None.gif
using  std::cout;
None.gif
using  std::endl;
None.gif
None.giftemplate
< int  N >
None.gif
class  CalculateCycle
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif    
enum dot.gif{ count = CalculateCycle< N % 2 ? (N * 3 + 1) : (N / 2>::count + 1 };
ExpandedBlockEnd.gif}
;
None.gif
None.giftemplate 
<>
None.gif
class  CalculateCycle < 1 >
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif    
enum dot.gif{ count = 1 };
ExpandedBlockEnd.gif}
;
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
const int iNo = 22;
InBlock.gif    cout 
<< "Cycle length of " << iNo << " is = " 
InBlock.gif        
<< CalculateCycle<iNo>::count << endl;
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

2.The following is a smart list. Please add other necessary implements to make it work.

None.gif #include  < iostream >
None.gif
None.giftemplate
< typename T >
None.gif
class  CListTemplate
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    T
* left;
InBlock.gif    T
* right;
InBlock.gif    
int elem;
ExpandedBlockEnd.gif}
;
None.gif
class  CMyList :  public  CListTemplate < CMyList >
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    size_t size;
ExpandedBlockEnd.gif}
;
None.gif
None.giftemplate
< class  T >
None.gif
class  CList :  public  T
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif T
* Head;
ExpandedBlockEnd.gif}
;
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    CList
<CMyList> my_List;
InBlock.gif
InBlock.gif    
//my_List.left->left->right->elem = 10;
InBlock.gif
 return 0;
ExpandedBlockEnd.gif}


 

转载于:https://www.cnblogs.com/llf/archive/2006/05/17/402120.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值