c++模板杂谈

时间:2020-02-01 10:42:18   收藏:0   阅读:88

说白了,模板就是搭个函数,类的框架,具体实现的时候往里面填充内容

#include <iostream>
#include<string.h>
using namespace std;


template <class TYPE>
TYPE returnmax(TYPE x, TYPE y)
{
    return (x>y)?x:y;
}

char*  returnmax(char* x,char* y)
{
    return (strcmp(x,y)<0)?x:y;
}


int main()
{
    cout << "Hello World!" << endl;
    char* chorum2 ="你你你你要跳舞吗";
    char* chorum1 ="每当浪潮来临的时候,你会不会也伤心";
    cout<<returnmax(chorum1,chorum2)<<endl;
    cout<<returnmax(99,888)<<endl;
    return 0;
}

输出结果:

技术图片

目录结构

技术图片

 

#ifndef XA_H
#define XA_H
#include<iostream>
#include<string.h>
using namespace std;

template <class sometype>
class xa
{
public:
    xa(sometype x)
    {
        myx = x;
    }
    void showme()
    {
        cout<<this->myx<<endl;
    }
private:
    sometype myx;
};

#endif // XA_H
#include <iostream>

using namespace std;
#include "xa.h"
int main()
{
    xa<char*> myx("瑞典鹰狮战斗机");
    myx.showme();
    xa<int> another(888);
    another.showme();

    //cout << "Hello World!" << endl;
    return 0;
}

输出结果:

技术图片

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!