第六章テンプレート

4329 ワード

6.1実習の目的
  • クラステンプレートの定義を熟知している.
  • 関数テンプレートの定義とパラメータのマッチングを熟知している.3)スタックの基礎知識を理解する.

  • 6.2実習任務
    6.2.1実習任務一
    1)
    #include 
    using namespace std;
    template
    T average( T * pd, int n){
        T sum = 0;
        int i = 0;
        while(i
        :
    Average of a: 5.5
    Average of b: 5
    

    2)
    #include  
    using namespace std;
    template
    T1 sum(T2 a, T3 b){
        return a+b;
    }
    int main(){
        auto r1 = sum(2,5.5);
        cout<(2.3,5.6);
        cout<(2.4, 5.5);
        cout<
        :
    result1: 7.5
    result2: 7
    result3: 7
    [Finished in 0.3s]
    
    #include 
    using namespace std;
    class Stack{
    public: 
        Stack(int size = 100);
        ~Stack();
        void push(int data);
        int pop();
        bool isEmpty();
        bool isFull();
    private:
        int *pData;
        int stackSize;
        int top;
    };
    Stack::Stack(int size){
        pData = new int[size];
        stackSize = size;
        top = -1;
    }
    Stack::~Stack(){
        delete[]pData;
    }
    void Stack::push(int data){
        if(isFull()){
            cout<
        :
    2 3 1 Stack is full!
    [Finished in 0.3s]
    
    #include 
    #include "Stack.h"
    using namespace std;
    template 
    class Stack
    {
    public:
        Stack(int size = 100){
            stacksize = size;
            pData = new int[stacksize];
        }
        ~Stack(){
            delete[] pData;
        }
        void push(T data){
            if(isfull()){
                cout< s;
        for(int i=0 ;i<100; i++){
            s.push(i);
        }
        cout<
    #include
    using std::cout;using std::endl;using std::cin;using std::ostream;
    template 
    class Array1D{
    public:
      Array1D(int newSize);
      Array1D(T *p,int newSize);
      Array1D(const Array1D & a);
      ~Array1D();
      int getSize() const;
      T max()const;
      void reverse();
      const T& operator[](int index) const;
      T& operator[](int index);
      Array1D& operator = (const Array1D& a);
      friend ostream& operator<< (ostream& out,Array1D& t){
        for(int i=0;i
    Array1D::Array1D(int newSize):size(newSize){
      pData=new T[size];
    }
    template 
    Array1D::Array1D(T *p,int newSize):pData(p),size(newSize){}
    template 
    Array1D::Array1D(const Array1D  &a):size(a.size){
      pData=new T[size]; 
      for(int i=0;i
    Array1D::~Array1D(){
      delete[] pData;
    }
    template 
    int Array1D::getSize() const{
      return size;
    }
    template 
    T Array1D::max() const {
      if (size==0) {
        cout< Max) Max=pData[i];
      }
      return Max;
    }
    template 
    void Array1D::reverse(){
      for(int i=0;i
    const T& Array1D::operator[](int index) const{
      return pData[index];
    }
    template 
    T& Array1D::operator[](int index){
      return pData[index];
    }
    template 
    Array1D& Array1D::operator=(const Array1D& a){
      delete[] pData;
      size=a.size;
      pData=new T[size];
      for(int i=0;i array(5);
      for(int i=0;i
    Max:5
    1 2 3 4 5 
    1 2 3 4 5 
    [Finished in 0.5s]
    
    #include
    #include
    using namespace std;
    
    template
    auto sum(const T1& a,const T2& b)->decltype(a+b){
      return a+b;
    }
    int main(){
      char str[]{"helloworld"};
      cout<
    first:helloworld
    second:107
    third:hellow
    helloworld
    fourth:loworld
    [Finished in 0.4s]