ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:20.73KB ,
资源ID:3145540      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-3145540.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(模板实例.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

模板实例.docx

1、模板实例#include template T abs(T value) return value0? value: -value;void main() int nValue=-1,nResult; double dblValue=-1.2,dblResult; nResult=abs(nValue); dblResult=abs(dblValue); coutnValue nResultendl; coutdblValue dblResultendl;#include template class Max /声明类模板Max private: T item1, /类型为T,T在该类的对象生

2、成时具体化 item2, item3; public: Max() Max(T thefirst,T thesecond, T thethird); T GetMaxItem(); /求得3个元素中的最大值并按类型T返回 void SetItem(T thefirst,T thesecond, T thethird); /设置类中的3个元素的值 ;template Max:Max(T thefirst, T thesecond, T thethird):item1(thefirst),item2(thesecond),item3(thethird) template void Max:SetI

3、tem(T thefirst, T thesecond, T thethird) item1=thefirst; item2=thesecond; item3=thethird;template T Max:GetMaxItem() T maxitem; maxitem=item1item2 ? item1: item2; maxitem=maxitemitem3? maxitem: item3; return maxitem;void main() Max nmyMax(1,2,3); Max dblmyMax(1.2,1.3,-1.4); coutnmyMax.GetMaxItem()en

4、dl; coutdblmyMax.GetMaxItem()endl;或者#include template class Max /声明类模板Max private: T item1, /类型为T,T在该类的对象生成时具体化 item2, item3; public: Max() Max(T thefirst,T thesecond, T thethird); T GetMaxItem(); /求得3个元素中的最大值并按类型T返回 void SetItem(T thefirst,T thesecond, T thethird); /设置类中的3个元素的值 ;template Max:Max(T

5、thefirst, T thesecond, T thethird):item1(thefirst),item2(thesecond),item3(thethird) template void Max:SetItem(T thefirst, T thesecond, T thethird) item1=thefirst; item2=thesecond; item3=thethird;template T Max:GetMaxItem() T maxitem; maxitem=item1item2 ? item1: item2; maxitem=maxitemitem3? maxitem:

6、item3; return maxitem;void main() Max nmyMax(1,2,3); Max dblmyMax(1.2,1.3,-1.4); coutnmyMax.GetMaxItem()endl; coutdblmyMax.GetMaxItem()endl;#includeusing namespace std;/首先看结点组织,采用结点类,凡与结点数据和指针操作有关函数作为成员函数templateclass List;templateclass Node T info; /数据域 Node *link; /指针域public: Node(); /生成头结点的构造函数 N

7、ode(const T & data);/生成一般结点的构造函数 void InsertAfter(Node* P); /在当前结点后插入一个结点 Node* RemoveAfter(); /删除当前结点的后继结点,返回该结点备用 friend class List; /以List为友元类,List可直接访问Node的私有成员,与结构一样方便,但更安全;template Node:Node()link=NULL;template Node:Node(const T & data) info=data; link=NULL;templatevoid Node:InsertAfter(Node*

8、p) p-link=link; link=p;templateNode* Node:RemoveAfter() Node* tempP=link; if(link=NULL) tempP=NULL; /已在链尾,后面无结点 else link=tempP-link; return tempP;/再定义链表类,选择常用操作:包括建立有序链表、搜索遍历、插入、删除、取数据等templateclass List Node *head,*tail;/链表头指针和尾指针public: List(); /构造函数,生成头结点(空链表) List(); /析构函数 void MakeEmpty(); /清空

9、一个链表,只余表头结点 Node* Find(T data); /搜索数据域与data相同的结点,返回该结点的地址 int Length(); /计算单链表长度 void PrintList(); /打印链表的数据域 void InsertFront(Node* p); /可用来向前生成链表,在表头插入一个结点 void InsertRear(Node* p); /可用来向后生成链表,在表尾添加一个结点 void InsertOrder(Node *p); /按升序生成链表 Node*CreatNode(T data); /创建一个结点(孤立结点) Node*DeleteNode(Node*

10、p); /删除指定结点;templateList:List() head=tail=new Node();templateList:List() MakeEmpty(); delete head;templatevoid List:MakeEmpty() Node *tempP; while(head-link!=NULL) tempP=head-link; head-link=tempP-link; /把头结点后的第一个节点从链中脱离 delete tempP; /删除(释放)脱离下来的结点 tail=head; /表头指针与表尾指针均指向表头结点,表示空链template Node* Li

11、st:Find(T data) Node *tempP=head-link; while(tempP!=NULL&tempP-info!=data) tempP=tempP-link; return tempP; /搜索成功返回该结点地址,不成功返回NULLtemplateint List:Length() Node* tempP=head-link; int count=0; while(tempP!=NULL) tempP=tempP-link; count+; return count;templatevoid List:PrintList() Node* tempP=head-link

12、; while(tempP!=NULL) coutinfolink; coutendl;templatevoid List:InsertFront(Node *p) p-link=head-link; head-link=p; if(tail=head) tail=p;templatevoid List:InsertRear(Node *p) p-link=tail-link; tail-link=p; tail=p;templatevoid List:InsertOrder(Node *p) Node *tempP=head-link,*tempQ=head; /tempQ指向tempP前面

13、的一个节点 while(tempP!=NULL) if(p-infoinfo)break; /找第一个比插入结点大的结点,由tempP指向 tempQ=tempP; tempP=tempP-link; tempQ-InsertAfter(p); /插在tempP指向结点之前,tempQ之后 if(tail=tempQ) tail=tempQ-link;templateNode* List:CreatNode(T data)/建立新节点 Node*tempP=new Node(data); return tempP;templateNode* List:DeleteNode(Node* p) N

14、ode* tempP=head; while(tempP-link!=NULL&tempP-link!=p) tempP=tempP-link; if(tempP-link=tail) tail=tempP; return tempP-RemoveAfter(); /本函数所用方法可省一个工作指针,与InsertOrder比较int main() Node * P1; List list1,list2; int a16,i,j; cout请输入16个整数endl; for(i=0;iai; /随机输入16个整数 for(i=0;i16;i+) P1=list1.CreatNode(ai); l

15、ist1.InsertFront(P1);/向前生成list1 P1=list2.CreatNode(ai); list2.InsertRear(P1); /向后生成list2 list1.PrintList(); coutlist1长度:list1.Length()endl; list2.PrintList(); cout请输入一个要求删除的整数j; P1=list1.Find(j); if(P1!=NULL) P1=list1.DeleteNode(P1); delete P1; list1.PrintList(); coutlist1长度:list1.Length()endl; else

16、 cout未找到endl; list1.MakeEmpty();/清空list1 for(i=0;i16;i+) P1=list1.CreatNode(ai); list1.InsertOrder(P1);/升序创建list1 list1.PrintList(); return 0;vector a(n);/按需创建vector b(10, 1);/10个元素赋全,灵活的初始化vector c(b);/整体拷贝创建vector f(t, t+5);/异类拷贝创建迭代器定义:Vector:iteratorit;It为定义的迭代器,为指针类型;bbegin();迭代器指向第一个元素位置;bend(

17、);迭代器指向最后一个元素的后一个位置;bsize();求得向量里的元素数;vector d(b.begin(), b.begin()+3);/局部拷贝创建d为b的前个元素a.assign(100);/动态扩容至100个元素a.assign(b.begin(), b.begin()+3);/ b的前3个元素赋给aa.assign(4,2);/ a向量含4个元素,全初始化为2int x = a.back();/ a的最后一个元素赋给变量xa.clear();/ a向量清空(不再有元素)if(a.empty() cout”empty”;/ a判空操作int y = a.front();/ a的第一

18、个元素赋给变量ya.pop_back();/删除a的最后一个元素a.push_back(5);/ a最后插入一个元素,其值为5a.resize(10);/ a元素个数调至10。多删少补,其值随机a.resize(10,2);/a元素个数调至10。多删少补,新添元素初值为2if(a=b) cout”equal”;/ a与b的向量比较操作1.基本操作vector是向量类型,她是一种对象实体,具有值,所以可以看作是变量。她可以容纳许多其他类型的相同实体,如若干个整数,所以称其为容器。Vector是C+STL(标准模板类库)的重要一员,使用她时,只要包括头文件#include即可。vector可以有四

19、种定义方式: vector是模板形式,尖括号中为元素类型名,她可以是任何合法的数据类型。 (1)vector a(10);/定义了10个整数元素的向量,但并没有给出初值,因此其值是不确定的。 (2)vector b(10,1);/定义了10个整数元素的向量,且给出每个元素的初值为1。这种形式是数组望尘莫及的,/数组只能通过循环来成批的赋给相同初值。 (3)vector c(b);/用另一个现成的向量来创建一个向量。 (4)vector d(b.begin(),b.begin()+3);/定义了其值依次为b向量中第0到第2个(共3个)元素的向量。 因此,创建向量时,不但可以整体向量复制性赋值,还

20、可以选择其他容器的部分元素来定义向量和赋值。 特别的,向量还可以从数组获得初值。例如:int a8=2007,9,24,2008,10,14,10,5;vector va(a,a+8);上面第(4)种形式的b.begin() 、b.end()是表示向量的起始元素位置和最后一个元素之外的元素位置。向量元素位置也属于一种类型,称为遍历器。遍历器不单表示元素位置,还可以在容器中前后挪动。每种容器都有对应的遍历器。向量中的遍历器类型为:vector:iterator因此要输入向量中所有元素,可以有两种循环控制方式:for(int i=0; ia.size(); +i) /第一种方法 coutai ;

21、for(vector:iterator it=a.begin(); it!=a.end(); +it) /第二种方法 cout*it ;第一种方法是下标方式,ai是向量元素操作,这种形式和数组一样; 第二种方法是遍历器方式,*it是指针间访形式,它的意义是it所指向的元素值。a.size()是向量中元素的个数,a.begin()表示向量的第一个元素,这种操作方式是一个对象捆绑一个函数调用,表示对该对象进行某个操作。 类似这样的使用方式称为调用对象a的成员函数,这在对象化程序设计中很普遍。向量中的操作都是通过使用成员函数来完成的。它的常用操作有:a.assign(b.begin(), b.beg

22、in()+3); /b向量的02元素构成向量赋给aa.assign(4,2); /使a向量只含03元素,且赋值为2int x=a.back(); /将a的最后一个向量元素值赋给整数型变量xa.clear();/a向量中元素清空(不再有元素)if(a.empty() coutempty; /a.empty()经常作为条件,判断向量是否为空int y=a.front(); /将a的第一个向量元素值赋给整型变量ya.pop_back(); /删除a向量的最后一个元素a.push_back(5); /在a向量最后插入一个元素,其值为5a.resize(10); /将向量元素个数调至10个。多则删,少则

23、补,其值随机a.resize(10,2); /将向量元素个数调至10个。多则删,少则补,其值为2if(a=b) coutepual; /向量的比较操作还有 !=, , , =除此之外,还有元素的插入与删除、保留元素个数、容量观察等操作。用 STL vector 来创建二维数组通常情况下,采用向量创建二维数组的方法有两种:方法一:1 int row=3;/行数2 int column=4;/列数3 vectorvector arry2(row);/定义一个三行四列的数组4 for (int i=0;i arry2.size( );i+)5 6 arry2i.resize(column);7 方法

24、二:vectorvector arry2(row,vector(column);#include #include #include using namespace std; bool cmp(int a,int b);void main() int i; vector a; for(i=1;i=10;i+) a.push_back(i); for(i=0;ia.size();i+) coutai; coutendl; vector :iterator it; for(it=a.begin();it!=a.end();it+) cout*it; coutendl; int x=a.back()

25、; coutxendl; vector c(a); int y=c.front(); coutyendl; a.pop_back(); for(it=a.begin();it!=a.end();it+) cout*it; coutendl; a.resize(12); for(it=a.begin();it!=a.end();it+) cout*it; coutendl; a.resize(15,2); for(it=a.begin();it!=a.end();it+) cout*it; coutendl; c.clear(); if(c.empty() cout向量已为空!n; vector d(a.begin(),a.begin()+5); for(it=d.begin();it!=d.end();it+) cout*it; coutendl; s

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2