c++模板杂谈

时间:2020-02-01 09:52:07   收藏:0   阅读:54

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

#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;
}

输出结果:

技术分享图片

 

原文:https://www.cnblogs.com/saintdingspage/p/12247418.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!