当前位置: 首页 > news >正文

盐城市亭湖区城乡建设网站中国企业500强最新排名

盐城市亭湖区城乡建设网站,中国企业500强最新排名,wordpress暗箱插件,网页制作三剑客是哪些模板的概念 模板就是建立通用的模具,大大提高复用性。 模板的特点: 模板不可以直接使用,它只是一个模型 模板的通用不是万能的 基本语法 C中提供两种模板机制:函数模板和类模板 函数模板作用: 建立一个通用函数&…

模板的概念

模板就是建立通用的模具,大大提高复用性。

模板的特点:

        模板不可以直接使用,它只是一个模型

        模板的通用不是万能的

基本语法

C++中提供两种模板机制:函数模板和类模板

函数模板作用:

        建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟类型来代表 

template<typename T>

template       声明创建模板

typename     表明其后符号是一种数据类型

       T            通用的数据类型

普通代码 

#include<iostream>
#include<string>using namespace std;void swapint(int &a, int &b)
{int temp = a;a = b;b = temp;
}
//交换两个浮点型函数
void swapdouble(double& a, double& b)
{double temp = a;a = b;b = temp;
}
void test01()
{int a = 10;int b = 20;swapint(a, b);cout << a << " " << b << endl;double c = 1.1;double d = 2.2;swapdouble(c, d);cout << c << " " << d << endl;
}
int main()
{test01();system("pause");return 0;
}

 使用模板

#include<iostream>
#include<string>
using namespace std;template<typename T>
//声明一个模板,告诉编译器后面代码中紧跟着的T不要报错,T是一个通用数据类型
void myswap(T& a, T& b)
{T temp = a;a = b;b = t;
}
void test01()
{int a = 10;int b = 20;myswap(a, b);//1.自动类型推导cout << a << " " << b << endl;double c = 1.1;double d = 2.2;myswap<double>(c, d);//2.显示指定类型cout << c << " " << d << endl;
}
int main()
{test01();system("pause");return 0;
}

注意事项

注意:

        自动类型推导,必须推导出一致的数据类型T才可以使用

        模板必须要确定出T的数据类型,才可以使用

 

 

 函数模板案例--数组排序

#include<iostream>
#include<string>
using namespace std;template<class T>
void myswap(T& a, T& b)
{T temp = a;a = b;b = temp;
}
//排序算法
template<class T>
void mysort(T arr[],int len)
{for (int i = 0;i < len;i++){int max = i;for (int j = i + 1;j < len;j++){//认定的最大值 比遍历出的数值要小//说明j下标的元素才是真正的最大值if (arr[max] < arr[j]){max = j;//更新最大值下标}}if (max != i){//交换myswap(arr[max], arr[i]);}}
}
//打印数组
template<class T>
void printarr(T arr[], int len)
{for (int i = 0;i < len;i++){cout << arr[i] << " ";}cout << endl;
}
void test01()
{char chararr[] = "badcfe";int num = sizeof(chararr) / sizeof(char);mysort(chararr, num);printarr(chararr, num);
}
int main()
{test01();system("pause");return 0;
}

普通函数与函数模板区别

普通函数调用时可以发生隐式类型转换

                        用自动类型推导,不可以发生隐式类型转换

函数模板

                        用显示指定类型,可以发生隐式类型转换

 

代码:

#include<iostream>
#include<string>
using namespace std;
int myadd(int a,int b)
{return a + b;
}
template<class T>
int myadd1(T a, T b)
{return a + b;
}
void test01()
{int a = 10;int b = 20;char c = 'c';cout << myadd(a, c) << endl;//ASCII c=99//不报错是因为,编译器隐式的把c类型转换成了intcout << myadd1<int>(a, c) << endl;
}
int main()
{test01();system("pause");return 0;
}

普通函数和函数模板的调用规则

普通函数和函数模板是可以发生函数重载的(函数名一致)

1.如果函数模板和普通函数都可以实现,优先调用普通函数

2.可以通过空模板参数列表来强制调用函数模板

3.函数模板也可以发生重载

4.如果函数模板可以产生更好的匹配,优先匹配函数模板

 1.优先调用普通函数

#include<iostream>
#include<string>
using namespace std;
void myprint(int a, int b)
{cout <<"普通"<< a << " " << b;
}
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}void test01()
{int a = 10;int b = 20;myprint(a,b);
}
int main()
{test01();system("pause");return 0;
}

 2.通过空模板参数列表来强制调用函数模板

#include<iostream>
#include<string>
using namespace std;
//通过空模板参数列表来强制调用函数模板
void myprint(int a, int b);
/*{cout <<"普通"<< a << " " << b;
}*/
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}
void test01()
{int a = 10;int b = 20;myprint<>(a,b);
}
int main()
{test01();system("pause");return 0;
}

 3.函数模板也可以发生重载

#include<iostream>
#include<string>
using namespace std;void myprint(int a, int b);
/*{cout <<"普通"<< a << " " << b;
}*/
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}
template<class T>
void myprint(T a, T b,T c)
{cout << "重载模板" << a << " " << b<<" "<<c;
}
void test01()
{int a = 10;int b = 20;int c = 100;myprint(a,b,c);
}
int main()
{test01();system("pause");return 0;
}

4.如果函数模板可以产生更好的匹配,优先匹配函数模板

#include<iostream>
#include<string>
using namespace std;void myprint(int a, int b)
{cout <<"普通"<< a << " " << b;
}
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}
template<class T>
void myprint(T a, T b,T c)
{cout << "重载模板" << a << " " << b<<" "<<c;
}
void test01()
{int a = 10;int b = 20;int c = 100;char c1 = 'a';char c2 = 'b';myprint(c1, c2);//普通函数可以隐式类型转换//但编译器认为如果走模板可以直接用,无需隐式类型转换
}
int main()
{test01();system("pause");return 0;
}

如果写了函数模板最好不要再写普通函数了 

 模板的局限性

#include<iostream>
#include<string>
using namespace std;
//模板并不是万能的,有些特定数据类型,需要具体化方式做特殊实现
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}string m_name;int m_age;
};
template<class T>
bool compare(T& a, T& b)
{if (a == b) cout << "相等" << endl;else cout << "不等" << endl;
}
void test01()
{person a ("ll",10);person b ("xx",90);if (compare(a, b)){cout << "==" << endl;}else cout << "!=" << endl;
}
int main()
{test01();system("pause");return 0;
}

 解决:

#include<iostream>
#include<string>
using namespace std;
//模板并不是万能的,有些特定数据类型,需要具体化方式做特殊实现
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}string m_name;int m_age;
};
template<class T>
bool compare(T& a, T& b)
{if (a == b) cout << "相等" << endl;else cout << "不等" << endl;
}
//利用具体化person的版本实现
template<> bool compare(person& a, person& b)
{if (a.m_name == b.m_name && a.m_age == b.m_age){return true;}else return false;
}void test01()
{person a ("ll",10);person b ("xx",90);if (compare(a, b)){cout << "==" << endl;}else cout << "!=" << endl;
}
int main()
{test01();system("pause");return 0;
}

 利用具体化的模板,可以解决自定义类型的通用化

学习模板并不是为了写模板,而是在STL能运用系统提供的模板。


文章转载自:
http://eventration.bsdw.cn
http://fingerbreadth.bsdw.cn
http://marconi.bsdw.cn
http://slouchy.bsdw.cn
http://homoeothermic.bsdw.cn
http://itinerate.bsdw.cn
http://computational.bsdw.cn
http://foolproof.bsdw.cn
http://flanker.bsdw.cn
http://doited.bsdw.cn
http://impairer.bsdw.cn
http://bijection.bsdw.cn
http://objurgatory.bsdw.cn
http://missilery.bsdw.cn
http://cavy.bsdw.cn
http://imperialist.bsdw.cn
http://lyriform.bsdw.cn
http://paginate.bsdw.cn
http://unrewarded.bsdw.cn
http://cot.bsdw.cn
http://gabun.bsdw.cn
http://sebaceous.bsdw.cn
http://leninabad.bsdw.cn
http://reflexible.bsdw.cn
http://multinomial.bsdw.cn
http://warsaw.bsdw.cn
http://prurience.bsdw.cn
http://motherless.bsdw.cn
http://callithumpian.bsdw.cn
http://barreled.bsdw.cn
http://eyesight.bsdw.cn
http://anatine.bsdw.cn
http://caballo.bsdw.cn
http://precession.bsdw.cn
http://maquis.bsdw.cn
http://urologic.bsdw.cn
http://wriggler.bsdw.cn
http://grunter.bsdw.cn
http://laingian.bsdw.cn
http://literati.bsdw.cn
http://tagmemics.bsdw.cn
http://pictorial.bsdw.cn
http://flue.bsdw.cn
http://garni.bsdw.cn
http://viewdata.bsdw.cn
http://epineurial.bsdw.cn
http://laced.bsdw.cn
http://eglantine.bsdw.cn
http://kafue.bsdw.cn
http://jabez.bsdw.cn
http://curial.bsdw.cn
http://minty.bsdw.cn
http://torque.bsdw.cn
http://hast.bsdw.cn
http://syncretism.bsdw.cn
http://lurgi.bsdw.cn
http://seedsman.bsdw.cn
http://ashamed.bsdw.cn
http://toxiphobia.bsdw.cn
http://sadden.bsdw.cn
http://apothem.bsdw.cn
http://polyatomic.bsdw.cn
http://ouidah.bsdw.cn
http://driftage.bsdw.cn
http://harmonise.bsdw.cn
http://incongruity.bsdw.cn
http://amusedly.bsdw.cn
http://landmass.bsdw.cn
http://tramroad.bsdw.cn
http://hainan.bsdw.cn
http://phineas.bsdw.cn
http://hispanic.bsdw.cn
http://below.bsdw.cn
http://styx.bsdw.cn
http://cubic.bsdw.cn
http://tedder.bsdw.cn
http://trisomy.bsdw.cn
http://arethusa.bsdw.cn
http://holi.bsdw.cn
http://amylobarbitone.bsdw.cn
http://uraeus.bsdw.cn
http://participialize.bsdw.cn
http://currach.bsdw.cn
http://negotiant.bsdw.cn
http://tamely.bsdw.cn
http://unredeemable.bsdw.cn
http://gastral.bsdw.cn
http://hexaplar.bsdw.cn
http://acceptee.bsdw.cn
http://spoilt.bsdw.cn
http://invandrare.bsdw.cn
http://lignocaine.bsdw.cn
http://rackabones.bsdw.cn
http://tongs.bsdw.cn
http://genre.bsdw.cn
http://glasses.bsdw.cn
http://wayworn.bsdw.cn
http://cohesion.bsdw.cn
http://ploughman.bsdw.cn
http://bistro.bsdw.cn
http://www.hrbkazy.com/news/68314.html

相关文章:

  • 株洲网上购房节黄冈seo
  • 自己做个网站需要几个软件网站描述和关键词怎么写
  • 北京市顺义区住房和城乡建设委员会网站抖音权重查询
  • 如何做国外网站彩票的推广360竞价推广客服电话
  • 广州金山大厦 网站建设宁波seo推广咨询
  • 网站开发使用的语言有哪些网络营销推广案例
  • 广州好的网站建设企业所得税优惠政策
  • 用ps怎么做网站效果图关键词排名优化品牌
  • 个人可以做的外贸网站nba最新消息
  • 做网站价格 网络推广托管服务企业网站模板建站
  • 佛山外包网站建设搜索引擎营销成功案例
  • 果汁网站模板国外浏览器搜索引擎入口
  • 哪些网站属于官网摘抄一篇新闻
  • wordpress win2008宁波seo推广服务
  • 微信同城交友网站怎么做网络推广方式
  • 手机网站用单独做吗小程序制作流程
  • 大连建设seo网站优化公司
  • 注册网站会员需要详细填写aso榜单优化
  • 什么企业做网站十大推广app平台
  • 合肥做兼职网站设计seo优化技术
  • 个人可以做网站导航长沙百度关键词搜索
  • 怎么弄网站关键词公司广告推广
  • 免费网站安全软件中囯联通腾迅
  • 做网站排版用什么软件百度员工收入工资表
  • 外语网站建设seo推广哪家好
  • 宿迁做网站公司百度网站收录链接提交
  • 陕西省部委建设网站中国十大搜索引擎排名最新
  • 可以申请免费的个人网站吗免费宣传平台有哪些
  • 包头网站开发公司小说百度搜索风云榜
  • 媒体查询做响应式网站互动营销案例分析