资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
程序设计艺术与方法课程实验报告实验名称实验一 STL的熟悉与使用姓名系院专业计算机科 学与技术班级学号实验日期5.29指导教师徐本柱成绩一、实验目的和要求1. 掌握C+中STL的容器类的使用;2. 掌握C+中STL的算法类的使用.二、实验预习内容1. 预习ICPC讲义,大致了解STL的相关内容。2. 了解STL中一些类vector list类的使用方法3. 了解泛型算法的使用三、实验项目摘要1 .练习vector和list的使用2.练习泛型算法的使用四、实验结果与分析(源程序及相关说明)使用一个程序显示list与vector的使用,以及泛型算法的使用其中加入vector的排序算法定义一个空的vector,元素类型为int,生成10个随机数插入到vector中,用迭代器遍历vector 输出其中的兀素值。在vector头部插入一个随机数,用迭代器遍历vector并输出其中的兀素值。 用泛型算法find查找某个随机数,如果找到便输出,否则将此数插入vector尾部。用泛型算法sort 将vector排序,用迭代器遍历vector并输出其中的元素值。删除vector尾部的元素,用迭代器遍 历vector并输出其中的元素值。将vector清空List重复vector的操作vcctDril ;19106 1B456 25367 12517 8265 26661 11562 11323 6329 229273122& 15106 1S45A 25367 12517 3265 26661 11562 11323 6329 22927 谙输入祢要査找的数:3L226查務帚功I臣抄为:兀,6329 3265 11328 _L?bJ L2517 13456 19106 22927 25367 26661 3122631226 266fil 25367 22927 1910fi LB456 12517 11562 11322 2265 6329 删除最岳一个元童3L226 26661 25367 22927 19106 LB456 12517 11562 1L328 8265城大值,31226取.慎弟茁Li st练习:19142 5616 亦&5 150&9 23421 4319 13031 11896 24117 2083429599 19142 5616 25255 15069 23121 4319 13031 11396 24117 20834 请输入祢要查找的甑!?岂社;却买9 _9K225265 1.刃诜 2312_ 3K 打疋:1LE96 2111712J.Z.#include stdafx.h#includeiostream#includectime#includevector#includelist#includeusing namespace std;bool comper(int a, int b)return (a b);/vec tor 练习void 0UTV(vectorint v)vec torin t:it era tor it = v.begin();for (; it != v.end(); +it)cout(*it);cout endl;void FindV(vectorint &exp)int x;vectorint:iterator L;cou t请输入你要查找的数: x;L = find(exp.begin(), exp.end(), x); if (L != exp.end()cou t查找成功! endl; cout该数为:*L endl;elsecout无该数! endl; exp.push_back(x);cou t输出结果:; OUTV(exp);void SortV(vectorint &exp)cout升序:endl; sort( exp.begin(), exp.end();OUTV(exp);cout降序endl;sort( exp.begin(), exp.end(),comper); OUTV(exp);void DeldetV (vectorint&exp)cout删除最后一个元素endl; exp.pop_back();OUTV(exp);void V()vectorint expl; srand(in t)time(O);cout vector练习:endl;for (int i = 0; i 10; i+) exp1.push_back(rand(); /插入 10个随机数OUTV(expl);exp1.inse rt( exp1.begin(), rand();OUTV(exp1);FindV(exp1);Sor tV(exp1);Deld et V(exp1);cout最大值: exp10 endl;cout最小值: exp1exp1.size()T endl; exp1.clear();/清空迭代器/Lis t练习void OUTL(listint v)lis tint:it era tor it = v.begin();for (; it != v.end(); +it)cout(*it) ;cout endl;void FindL(listint &exp)int x;listint:iterator L;cou t x;L = find(exp.begin(), exp.end(), x); if (L != exp.end()cou t查找成功! endl; cout该数为:*L endl;elsecout无该数! endl; exp.push_back(x);COU t 输出结果:; OUTL(exp);void SortL(listint &exp)cout排序:endl;exp.so rt();OUTL(exp);void DeldetL(listint &exp)cou t删除最后一个元素endl; exp.pop_back();OUTL(exp);void L()listint exp2;srand(in t)time(O);cout List练习:endl;for (int i = 0; i 10; i+) exp2.push_back(rand(); /插入 10个随机数OUTL(exp2);exp2.push_fron t( rand();OUTL(exp2);FindL(exp2);Sort L(exp2);Delde tL(exp2);/清空迭代器exp2.clear();一
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号