zoukankan      html  css  js  c++  java
  • c++ 模板

     
    #include <iostream>
    #include "SeqList.h"

    using namespace std;

    int main()
    {
         SeqList<int> test(15);
         int array[15]={2,5,8,1,9,9,7,6,4,3,2,9,7,7,9};
         for(int i=0;i<15;i++){
              test.Insert(array[i],0);
    }
         test.Insert(1,0);
         cout<<(test.Find(0)?"can't be found ":"Be found ")<< 0 << endl<<endl;
         test.Remove(7);
         test.Print();
         test.Remove(9);
         test.Print();
         test.Remove(0);
         test.Print();
         return 0;
    }

    #ifndef SEQLIST_H_INCLUDED
    #define SEQLIST_H_INCLUDED
    #include<iostream>
    using namespace std;
    const int DefaultSize=100;

    template <class elemType>
    class SeqList{
    public:
         SeqList(int sz=DefaultSize)
              :m_nmaxsize(sz),m_ncurrentsize(-1){
              if(sz>0){
                   m_elements=new elemType[m_nmaxsize];
              }
         }
         ~SeqList()
         {
              delete[] m_elements;
         }
         int Length() const
         {                         //get the length
              return m_ncurrentsize+1;
         }
         int Find(elemType x) const;                    //find the position of x
         int IsElement(elemType x) const;          //is it in the list
         int Insert(elemType x,int i);               //insert data
         int Remove(elemType x);                         //delete data
         int IsEmpty()
         {
              return m_ncurrentsize==-1;
         }
         int IsFull()
         {
              return m_ncurrentsize==m_nmaxsize-1;
         }
         elemType Get(int i)
         {                         //get the ith data
              return i<0||i>m_ncurrentsize?(cout<<"can't find the element"<<endl,0):m_elements[i];
         }
         void Print();

    private:
         elemType *m_elements;
         const int m_nmaxsize;
         int m_ncurrentsize;
    };

    template <class elemType> int SeqList<elemType>::Find(elemType x) const{
         for(int i=0;i<m_ncurrentsize;i++)
              if(m_elements[i]==x)
                   return i;
         cout<<"can't find the element you want to find"<<endl;
         return -1;
    }

    template <class elemType> int SeqList<elemType>::IsElement(elemType x) const{
         if(Find(x)==-1)
              return 0;
         return 1;
    }

    template <class elemType> int SeqList<elemType>::Insert(elemType x, int i){
         if(i<0||i>m_ncurrentsize+1||m_ncurrentsize==m_nmaxsize-1){
              cout<<"the operate is illegal"<<endl;
              return 0;
         }
         m_ncurrentsize++;
         for(int j=m_ncurrentsize;j>i;j--){
              m_elements[j]=m_elements[j-1];
         }
         m_elements[i]=x;
         return 1;
    }

    template <class elemType> int SeqList<elemType>::Remove(elemType x){
         int size=m_ncurrentsize;
         for(int i=0;i<m_ncurrentsize;){
              if(m_elements[i]==x){
                   for(int j=i;j<m_ncurrentsize;j++){
                        m_elements[j]=m_elements[j+1];
                   }
                   m_ncurrentsize--;
                   continue;
              }
              i++;
         }
         if(size==m_ncurrentsize){
              cout<<"can't find the element you want to remove"<<endl;
              return 0;
         }
         return 1;
    }

    template <class elemType> void SeqList<elemType>::Print(){
         for(int i=0;i<=m_ncurrentsize;i++)
              cout<<i+1<<": "<<m_elements[i]<<endl;
         cout<<endl<<endl;
    }
    #endif // SEQLIST_H_INCLUDED
  • 相关阅读:
    VINS bug 调试 : undefined reference to `cv::FileStorage::FileStorage(std::__cxx11::basic_string<char, std::char_traits<char>,
    Fundamental Matrix
    const和指针数组
    结构体的嵌套,结构体内定义结构体。
    第4章:动态规划
    第3章:有限马尔科夫决策过程
    吴恩达深度学习中reshape图片数据的用法
    Logistic 回归Loss函数与交叉熵、极大似然估计 关系
    Logistic 回归(吴恩达)
    强化学习Sutton (Reinforcement Learning : An introduction )文章概括和总结
  • 原文地址:https://www.cnblogs.com/jianfengyun/p/5005318.html
Copyright © 2011-2022 走看看