zoukankan      html  css  js  c++  java
  • 数据结构0类模板的使用

    类模板的使用

    #include <iostream>
    #include <conio.h>
    #include <string>
    #define N 3
    using namespace std;
    
    template <class numtype>
    class Swap{
        public :
        Swap(numtype a,numtype b){x=a;y=b;}
    
        numtype ___(){
             temp=x;
             x=y;
             y=temp;
             
             return x;
        }
       
        //test
        numtype ____(){
            return x;
        }
        //test
        private :
        numtype x,y;
        numtype temp;
    };
    
    template <class numtypeeee>
    class Seh_arr{
        public:
            Seh_arr(numtypeeee a[N]){
                for(int i = 0; i < N; ++i){
                    *(x+i)=*(a+i);
                }
            }
            numtypeeee seh(){
                cout << "input the Number of the element you want:";
                cin >> T;
                return x[T-1];
            }
        private:
            numtypeeee x[N];
            int T;
            
    };
    
    int main(void){
        
        
        Swap<int > wap1(3,7);
        cout<<wap1.___( )<<" "<<wap1.____( )<<"--change int."<<endl;
       
        Swap<double > wap2(45.78,93.6);
        cout<<wap2.___( )<<" "<<wap2.____( )<<"--change float."<<endl;
       
        Swap<char> wap3('a','A'); 
        cout<<wap3.___( )<<" "<<wap3.____( )<<"--change char."<<endl;
        
        Swap<string> wap4("nice", "good");
        cout<<wap4.___( )<<" "<<wap4.____( )<<"--change char."<<endl;
        
        int a[N]={1,2,3};
        Seh_arr<int>_testInt(a);
        cout << _testInt.seh() << "--search int."<< endl;
        
        float b[N]={1.1, 2.2, 3.3};
        Seh_arr<float>_testFlo(b);
        cout << _testFlo.seh() << "--search float." << endl;
        
        char c[N]={'a', 'b', 'c'};
        Seh_arr<char>_testCha(c);
        cout << _testCha.seh() << "--search char." << endl;
        
        string d[N]={"my", "name", "is"};
        Seh_arr<string>_testStr(d);
        cout << _testStr.seh() << "--search string." << endl;
        
        getch();
       return 0;
    }
  • 相关阅读:
    BZOJ 1631 Cow Party
    BZOJ 1927 星际竞速
    BZOJ 4059 Non-boring sequences
    BZOJ 1562 变换序列
    BZOJ 4417 超级跳马
    484586
    背板问题之满包问题
    对01背包路径的记录
    带权值的图 BFS
    漫步校园 杭电1428
  • 原文地址:https://www.cnblogs.com/jzl123/p/6418699.html
Copyright © 2011-2022 走看看