C++程序设计基础一周目第十一天

1.模板类的使用

//Array.h
#ifndef ARRAY_H
#define ARRAY_H
template< typename T >
class Array
{
public:
	Array(int s);
	virtual ~Array();
	virtual const T& Entry(int index)const;
	virtual void Enter(int index, const T& value);
protected:
	int size;
	T* element;
};
template
   
   
    
    Array
    
    
     
     ::Array(int s)
{
	if (s > 1)
	{
		size = s;
	}
	else
	{
		size = 1;
	}
	element = new T[size];
}
template
     
     
      
      Array
      
      
       
       ::~Array()
{
	delete[] element;
}
template
       
       
         const T& Array 
        
          ::Entry(int index)const { return element[index]; } template 
         
           void Array 
          
            ::Enter(int index, const T& value) { element[index] = value; } #endif ARRAY_H 
           
          
         
       
      
      
     
     
    
    
   
   
#include 
   
   
    
    

#include "Array.h"

using namespace std;

void main()
{
	Array
    
    
     
     Intary(5);
	int i;
	for (i = 0; i < 5; i++)
	{
		Intary.Enter(i, i);
	}
	cout << "Integer Array:\n";
	for (i = 0; i < 5; i++)
	{
		cout << Intary.Entry(i) << "\t";
	}
	cout << "\n";
	Array
     
     
      
      Douary(5);
	for (i = 0; i < 5; i++)
	{
		Douary.Enter(i, i);
	}
	cout << "double Array:\n";
	for (i = 0; i < 5; i++)
	{
		cout << Douary.Entry(i) << "\t";
	}
	cout << endl;
	
	system("pause");
}
     
     
    
    
   
   
2.函数模板的话只要在函数前加上template<typename T>就好了

3.类模板可以做形参,但实际调用时是模板的实例化对象,拥有类模板参数的函数一定是函数模板

4.类模板可以是基类也可以是派生类

5.派生类模板

//BoundArray.h
#ifndef BOUNDARRAY_H
#define BOUNDARRAY_H
#include "Array.h"
template
   
   
    
    
class BoundArray : public Array
    
    
     
     
{
public:
	BoundArray(int low = 0, int height = 1);
	virtual const T& Entry(int index)const;
	virtual void Enter(int index, const T& value);
private:
	int min;
};
template
     
     
      
      BoundArray
      
      
       
       ::BoundArray(int low, int height) :Array
       
       
         (height - low + 1) { min = low; } template 
        
          const T& BoundArray 
         
           ::Entry(int index)const { return Array 
          
            ::Entry(index - min); } template 
           
             void BoundArray 
            
              ::Enter(int index, const T& value) { Array 
             
               ::Enter(index - min, value); } #endif BOUNDARRAY_H 
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
6.模板也有友元或被当成友元,记得在使用模板的时候说明模板<T>

7.STL-Standard Template Library-标准模板库,主要组件:容器container、迭代器iterator、算法algorithm

序列容器,提供顺序表的表示和操作

vector向量

deque双向列表

list双向链表

关联容器

set集合

multilist集合

map映射

multimap映射

容器适配器

stack栈,后进先出

queue队列,先进先出

priority_queue优先队列,优先级高的先出


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值