美文网首页
第六章 模板

第六章 模板

作者: DeepWeaver | 来源:发表于2017-11-23 10:08 被阅读25次

6.1 实习目的

  1. 熟悉类模板的定义;
  2. 熟悉函数模板的定义与参数匹配; 3) 了解栈的基础知识。

6.2 实习任务

6.2.1 实习任务一

1)

#include <iostream>
using namespace std;
template<typename T>
T average( T * pd, int n){
    T sum = 0;
    int i = 0;
    while(i<n)
        sum+=pd[i++];
    return sum/n;
}
int main()
{
    double a[] = {2.5, 4.5, 6.5, 8.5};
    cout<<"Average of a: "<<average(a, 4)<<endl;
    int b[] = {3,5,7,8};
    cout<<"Average of b: "<<average(b, 4)<<endl;
    return 0;
}
运行结果:
Average of a: 5.5
Average of b: 5

2)

#include  <iostream>
using namespace std;
template<typename T1, typename T2, typename T3>
T1 sum(T2 a, T3 b){
    return a+b;
}
int main(){
    auto r1 = sum<double, int, double>(2,5.5);
    cout<<"result1: "<<r1<<endl;
    auto r2 = sum<double, int, int>(2.3,5.6);
    cout<<"result2: "<<r2<<endl;
    auto r3 = sum<int>(2.4, 5.5);
    cout<<"result3: "<<r3<<endl;
    return 0;
}
运行结果:
result1: 7.5
result2: 7
result3: 7
[Finished in 0.3s]
#include <iostream>
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<<"Stack is full!\n";
        exit(0);
    }
    top++;
    pData[top] = data;
}
int Stack::pop(){
    if(isEmpty()){
        cout<<"Stack is empty!\n";
        exit(0);
    }
    int temp = pData[top];
    top--;
    return temp;
}
bool Stack::isEmpty(){
    return top == -1;
}
bool Stack::isFull(){
    return top == stackSize - 1;
}
int main(){
    int array[10] = {1,3,2,4};
    Stack s(4);
    int i;
    for(i=0; i<3; ++i){
        s.push(array[i]);
    }
    while(!s.isEmpty())
        cout<<s.pop()<<" ";
    for(i=0; i<10; i++)
        s.push(array[i]);
    cout<<endl;
    return 0;
}
运行结果:
2 3 1 Stack is full!
[Finished in 0.3s]
#include <iostream>
#include "Stack.h"
using namespace std;
template <typename T>
class Stack
{
public:
    Stack(int size = 100){
        stacksize = size;
        pData = new int[stacksize];
    }
    ~Stack(){
        delete[] pData;
    }
    void push(T data){
        if(isfull()){
            cout<<"Stack is full! Data "<<data<<"has not been pushed!"<<endl;
            exit(0);
        }
        else{
            top ++;
            pData[top] = data;
        }
    }
    T pop(){
        if(isempty()){
            cout<<"Stack is empty!"<<endl;
            exit(0);
        }
        else{
            return pData[top--];
        }
    }
    bool isempty(){
        return top == -1;
    }
    bool isfull(){
        return top == stacksize -1;
    }
    T sum(){
        T s = 0;
        for(int i=0; i<top+1; i++){
            s += pData[i];
        }
        return s;
    }
private:
    int stacksize = 0;
    T* pData;
    int top = -1;
};

int main()
{

    Stack<int> s;
    for(int i=0 ;i<100; i++){
        s.push(i);
    }
    cout<<s.sum()<<endl;
    cout<<s.pop();
    while(!s.isempty()){
        cout<<s.pop()<<" ";
    }
    return 0;
}
#include<iostream>
using std::cout;using std::endl;using std::cin;using std::ostream;
template <typename T>
class Array1D{
public:
  Array1D(int newSize);
  Array1D(T *p,int newSize);
  Array1D(const Array1D <T>& a);
  ~Array1D();
  int getSize() const;
  T max()const;
  void reverse();
  const T& operator[](int index) const;
  T& operator[](int index);
  Array1D<T>& operator = (const Array1D<T>& a);
  friend ostream& operator<< (ostream& out,Array1D<T>& t){
    for(int i=0;i<t.size;++i) out<<t[i]<<" ";
    return out<<endl;
  }
private:
  T *pData;
  int size;
};
template <typename T>
Array1D<T>::Array1D(int newSize):size(newSize){
  pData=new T[size];
}
template <typename T>
Array1D<T>::Array1D(T *p,int newSize):pData(p),size(newSize){}
template <typename T>
Array1D<T>::Array1D(const Array1D <T> &a):size(a.size){
  pData=new T[size]; 
  for(int i=0;i<size;++i) pData[i]=a[i];
}
template <typename T>
Array1D<T>::~Array1D(){
  delete[] pData;
}
template <typename T>
int Array1D<T>::getSize() const{
  return size;
}
template <typename T>
T Array1D<T>::max() const {
  if (size==0) {
    cout<<"No elem!\n";
    exit(0);
  }
  T Max=pData[0];
  for(int i=1;i<size;++i){
    if(pData[i] > Max) Max=pData[i];
  }
  return Max;
}
template <typename T>
void Array1D<T>::reverse(){
  for(int i=0;i<size/2;++i){
    swap(pData[i],pData[size-1-i]);
  }
}
template <typename T>
const T& Array1D<T>::operator[](int index) const{
  return pData[index];
}
template <typename T>
T& Array1D<T>::operator[](int index){
  return pData[index];
}
template <typename T>
Array1D<T>& Array1D<T>::operator=(const Array1D<T>& a){
  delete[] pData;
  size=a.size;
  pData=new T[size];
  for(int i=0;i<size;++i) pData[i]=a[i];
}
int main(){
  Array1D <int> array(5);
  for(int i=0;i<array.getSize();++i)
    array[i]=i+1;
  cout<<"Max:"<<array.max()<<endl;
  for(int i=0;i<array.getSize();++i)
    cout<<array[i]<<" ";
  cout<<endl;
  cout<<array;
  return 0;
}
Max:5
1 2 3 4 5 
1 2 3 4 5 
[Finished in 0.5s]
#include<iostream>
#include<string>
using namespace std;

template<typename T1, typename T2>
auto sum(const T1& a,const T2& b)->decltype(a+b){
  return a+b;
}
int main(){
  char str[]{"helloworld"};
  cout<<"first:"<<sum(string("hello"),"world")<<endl;
  cout<<"second:"<<sum(10,'a')<<endl;
  cout<<"third:"<<sum(string("hello"),'w')<<endl;
  cout<<str<<endl;
  cout<<"fourth:"<<sum(str,3)<<endl;
  return 0;
}
first:helloworld
second:107
third:hellow
helloworld
fourth:loworld
[Finished in 0.4s]

相关文章

网友评论

      本文标题:第六章 模板

      本文链接:https://www.haomeiwen.com/subject/iwjnvxtx.html