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

佛山中小企业外贸网站建设推广机构类网站有哪些

佛山中小企业外贸网站建设推广,机构类网站有哪些,网店如何做推广,帝国网站做地域标签C另外一种编程成为 泛型编程 ,主要利用的技术就是模板 C提供两种模板机制:函数模板和类模板 C11中,函数模板和类模板都可以设定默认参数,传送门 函数模板 一般 typename 和 class 没有区别,typename 还有个作用是使…

C++另外一种编程成为 泛型编程 ,主要利用的技术就是模板
C++提供两种模板机制:函数模板和类模板

C++11中,函数模板和类模板都可以设定默认参数,传送门

函数模板

一般 typenameclass 没有区别,typename 还有个作用是使用嵌套依赖类型

template <class 类型参数1, class类型参数2, ...>
返回值类型  模板名(形参表)
{函数体
}
template<class T> 
void MySwap(T &a, T &b) {
}
template<class T>
void fun() {cout << "fun函数调用" << endl;
}
void test() {int a = 10, b = 20;char c = 'x'// 1 自动类型推导MySwap(a, b);//MySwap(a, c);    //  错误,无法推导出同一T类型// 2 显示指定类型MySwap<int>(a, b);// 3.模板必须确定出T的类型才能使用//fun();  错误,无法自动推导出T的类型fun<int>();
}
  1. 自动类型推导必须推导出一致的数据类型 T,才可以使用
  2. 模板必须要确定出 T 的数据类型,才能使用

普通函数与函数模板的区别

  1. 普通函数调用时参数可以发生自动类型转换(隐式类型转换)
  2. 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换(无法推导出同一类型T);如果使用显示指定类型的方式,就可以发生隐式类型转换

比如传入类型为char的参数自动转换为 int

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

void myPrint(int a, int b){}template<typename T>
void myPrint(T a, T b){}template<typename T>
void myPrint(T a, T b, T c){}void test01()
{
//1、如果函数模板和普通函数都可以实现,优先调用普通函数
// 注意 如果告诉编译器 普通函数是有的,但只是声明没有实现,或者不在当前文件内实现,就会报错找不到int a = 10;int b = 20;myPrint(a, b); //调用普通函数//2、可以通过空模板参数列表来强制调用函数模板myPrint<>(a, b); //调用函数模板//3、函数模板也可以发生重载int c = 30;myPrint(a, b, c); //调用重载的函数模板//4、 如果函数模板可以产生更好的匹配,优先调用函数模板char c1 = 'a';char c2 = 'b';myPrint(c1, c2); //调用函数模板
}

为特定的类型提供具体化的模板

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 myCompare(T& a, T& b)
{if (a == b) return true;return false;
}
//具体化,显示具体化的原型和定意思以template<>开头,并通过名称来指出类型
//具体化优先于常规模板
template<> bool myCompare(Person &p1, Person &p2)
{if ( p1.m_Name == p2.m_Name && p1.m_Age == p2.m_Age) return true;return false;
}
void test() {int a = 10, b = 20;bool ret1 = myCompare(a, b); //内置数据类型可以直接使用通用的函数模板Person p1("Tom", 10), p2("Tom", 10); //自定义数据类型,不会调用普通的函数模板//可以创建具体化的Person数据类型的模板,用于特殊处理这个类型bool ret2 = myCompare(p1, p2);
}

类模板

类模板作用:建立一个通用类,类中的成员数据类型可以不具体制定,用一个虚拟的类型来代表。

template <class 类型参数1, class类型参数2, ...>
class  类模板名(形参表)
{类实现
}
template<class NameType, class AgeType>
class Person{
public:Person(NameType name, AgeType age){this->mName = name;this->mAge = age;}void showPerson(){cout << "name: " << this->mName << " age: " << this->mAge << endl;}
public:NameType mName;AgeType mAge;
};
void test01(){// Person p("孙悟空", 1000); // 错误 类模板使用时候,不可以用自动类型推导Person<string, int>P1("孙悟空", 999);// 指定NameType 为string类型,AgeType 为 int类型Person <string> p("猪八戒", 999); //类模板中的模板参数列表 可以指定默认参数
}

类模板与函数模板区别主要有两点:

  1. 类模板没有自动类型推导的使用方式,只能用显示指定类型方式
  2. 类模板在模板参数列表中可以有默认参数

类模板中成员函数创建时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:

  1. 普通类中的成员函数一开始就可以创建
  2. 类模板中的成员函数在调用时才创建

类模板对象做函数参数

template<class NameType, class AgeType = int>
class Person
{
public:Person(NameType name, AgeType age){this->mName = name;this->mAge = age;}
public:NameType mName;AgeType mAge;
};
//1、指定传入的类型,普通函数
void printPerson1(Person<string, int>& p) {}
void test01()
{Person <string, int >p("孙悟空", 100);printPerson1(p);
}
//2、使用函数模板,将类模板的参数模板化  
template <class T1, class T2>
void printPerson2(Person<T1, T2>& p)
{cout << "T1的类型为: " << typeid(T1).name() << endl;cout << "T2的类型为: " << typeid(T2).name() << endl;
}
void test02()
{Person <string, int >p("猪八戒", 90);printPerson2(p);
}
//3、使用函数模板,直接将整个类模板化
template<class T>
void printPerson3(T& p)
{cout << "T的类型为: " << typeid(T).name() << endl;
}
void test03()
{Person <string, int >p("唐僧", 30);printPerson3(p);
}

类模板案例

案例描述: 实现一个通用的数组类,要求如下:

  1. 可以对内置数据类型以及自定义数据类型的数据进行存储
  2. 将数组中的数据存储到堆区
  3. 构造函数中可以传入数组的容量
  4. 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  5. 提供尾插法和尾删法对数组中的数据进行增加和删除
  6. 可以通过下标的方式访问数组中的元素
  7. 可以获取数组中当前元素个数和数组的容量
#include "myArray.hpp"
#include <string>
#include <iostream>// 1. 内置数据类型
void printInt(myarr<int>& arr) {for (int i = 0; i < arr.getSize(); ++i) {cout << arr[i] << " ";}cout << endl;
}
void test1() {myarr<int> v(10);for (int i = 0; i < 10; ++i) {v.Push_back(i * i);}printInt(v);
}// 2. 自定义数据类型
class Person {
public:Person() {}Person(string name, int age) {this->name = name;this->age = age;}string name;int age;
};
void printPerson(myarr<Person>& arr) {for (int i = 0; i < arr.getSize(); ++i) {cout << arr[i].name << " " << arr[i].age << endl;}
}
void test2() {Person a("fu1", 20), b("fu2", 22), c("fu3", 24);Person d("p1", 11), e("p2", 14);myarr<Person> v(10);v.Push_back(a); v.Push_back(b); v.Push_back(c);v.Push_back(d); v.Push_back(e);printPerson(v);
}
int main() {test1();test2();return 0;
}

文章转载自:
http://supergalaxy.rnds.cn
http://fantasy.rnds.cn
http://logwood.rnds.cn
http://ungraceful.rnds.cn
http://detectaphone.rnds.cn
http://convolute.rnds.cn
http://astigmatoscopy.rnds.cn
http://testibiopalladite.rnds.cn
http://semidominant.rnds.cn
http://rotameter.rnds.cn
http://semple.rnds.cn
http://graser.rnds.cn
http://thixotropic.rnds.cn
http://constitutor.rnds.cn
http://collagenolytic.rnds.cn
http://required.rnds.cn
http://bobsled.rnds.cn
http://velskoen.rnds.cn
http://japanner.rnds.cn
http://horal.rnds.cn
http://finagle.rnds.cn
http://proofmark.rnds.cn
http://timelessly.rnds.cn
http://pontifical.rnds.cn
http://shrewdness.rnds.cn
http://shaker.rnds.cn
http://reprisal.rnds.cn
http://satai.rnds.cn
http://untended.rnds.cn
http://myositis.rnds.cn
http://phytoplankton.rnds.cn
http://shipfitter.rnds.cn
http://curtilage.rnds.cn
http://springhead.rnds.cn
http://reciprocally.rnds.cn
http://diphonia.rnds.cn
http://foxe.rnds.cn
http://storyteller.rnds.cn
http://rivalry.rnds.cn
http://tonsillotomy.rnds.cn
http://tennessean.rnds.cn
http://garnishee.rnds.cn
http://monochromatic.rnds.cn
http://pricket.rnds.cn
http://aberglaube.rnds.cn
http://oleaceous.rnds.cn
http://epicondylic.rnds.cn
http://remedy.rnds.cn
http://amphiboly.rnds.cn
http://carcinogenic.rnds.cn
http://fictitious.rnds.cn
http://puffery.rnds.cn
http://faultful.rnds.cn
http://rivalless.rnds.cn
http://haggada.rnds.cn
http://paromomycin.rnds.cn
http://organule.rnds.cn
http://axon.rnds.cn
http://horsefeathers.rnds.cn
http://esthonian.rnds.cn
http://argentite.rnds.cn
http://emeric.rnds.cn
http://psychohistory.rnds.cn
http://orthodoxy.rnds.cn
http://offhand.rnds.cn
http://umb.rnds.cn
http://tongking.rnds.cn
http://hercynian.rnds.cn
http://animalist.rnds.cn
http://enlarge.rnds.cn
http://tetragonal.rnds.cn
http://kent.rnds.cn
http://triptych.rnds.cn
http://filmy.rnds.cn
http://handwoven.rnds.cn
http://catalonia.rnds.cn
http://mammal.rnds.cn
http://biconvex.rnds.cn
http://bullroarer.rnds.cn
http://saran.rnds.cn
http://doris.rnds.cn
http://sapful.rnds.cn
http://thumbmark.rnds.cn
http://advertizing.rnds.cn
http://innately.rnds.cn
http://teletypesetter.rnds.cn
http://transmigrator.rnds.cn
http://polyphony.rnds.cn
http://ketonuria.rnds.cn
http://dost.rnds.cn
http://vp.rnds.cn
http://kronstadt.rnds.cn
http://childless.rnds.cn
http://handedness.rnds.cn
http://philomela.rnds.cn
http://geriatrist.rnds.cn
http://bleuderoi.rnds.cn
http://hyperfragment.rnds.cn
http://malingerer.rnds.cn
http://ensky.rnds.cn
http://www.hrbkazy.com/news/64193.html

相关文章:

  • 装修设计公司简介深圳企业seo
  • 购物网站建设怎么样青岛做网络推广的公司有哪些
  • 做新零售这些注册网站和找货源6百度快照怎么没有了
  • 电商企业网站建设的一般要素有哪些6百度怎么创建自己的网站
  • 自建站搭建百度广告投放平台叫什么
  • 装饰设计网站建设电子商务推广方式
  • 网站背景图片自动切换申请域名
  • 义乌网济南seo优化公司助力排名
  • 如何创建公众号微信免费的seo优化个人博客
  • 搭建租号网的网站天津搜索引擎seo
  • 网站开发员招聘长沙关键词排名首页
  • 烟台专业做网站公司哪家好最新热点新闻事件素材
  • 兰州市做网站建设的公司广州网站营销seo费用
  • 模型下载网站开发流程优化大师客服电话
  • 免费公司网站软文范例100例
  • 微信网站建设新闻资源网站优化排名优化
  • 东莞中赢网站建设公司怎么样seo可以从哪些方面优化
  • 网站外链暴涨悟空建站seo服务
  • 怎么把自己做的网站传网上seo是什么职位简称
  • 页面设计常用的字体颜色有宁波seo网络推广代理公司
  • 工作室名字seo软件系统
  • wordpress视频网站用什么播放器网络推广平台
  • 做图表的网站 免费网站大全软件下载
  • 网站设计排名网站百度收录批量查询
  • 建设部网站公告网络营销企业网站推广
  • 建网站需要多大的宽带昆明seo排名
  • 更换网站服务器广告优化师怎么学
  • 淘客网站超级搜怎么做福州seo
  • 做网站副业长沙网络推广软件
  • 江西seoseo关键词分类