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

鹤山做网站快速学制作网站

鹤山做网站,快速学制作网站,软件著作权登记证书,重庆市建设工程信息网安全1、set / multiset容器 1.1set基本概念 简介#xff1a;所有元素都会在插入时自动被排序 本质#xff1a;set和multiset属于关联式容器#xff0c;底层结构是用二叉树实现 set和multiset区别#xff1a; set不允许容器中有重复的元素 multiset允许容器中有重复的元素 …1、set / multiset容器 1.1set基本概念 简介所有元素都会在插入时自动被排序 本质set和multiset属于关联式容器底层结构是用二叉树实现 set和multiset区别 set不允许容器中有重复的元素 multiset允许容器中有重复的元素 1.2set构造和赋值 功能描述创建set容器以及赋值 构造 setT st; //默认构造函数: set(const set st); //拷贝构造函数 赋值 set operator(const set st);//重载等号操作符 #include iostream using namespace std; #includeset //set容器构造和赋值 //构造: //setT st; //默认构造函数: //set(const set st); //拷贝构造函数 //赋值: //set operator(const set st);//重载等号操作符 void printSet(setint s) {for (setint::const_iterator it s.begin(); it ! s.end(); it){cout *it ;}cout endl; }void test01() {//创建set容器setints1;//默认构造//插入数据只有insert方式s1.insert(10);s1.insert(40);s1.insert(30);s1.insert(20);s1.insert(30);//遍历容器//set容器特点所有元素插入时自动被排序//set容器不允许插入重复值printSet(s1);//拷贝构造setints2(s1);printSet(s2);//赋值setints3;s3 s2;printSet(s3); }int main() {test01();system(pause);return 0; } 输出结果 10 20 30 40 10 20 30 40 10 20 30 40 请按任意键继续. . . 1.3set大小和交换 功能描述统计set容器大小以及交换set容器 函数原型: size(); //返回容器中元素的数目 empty(); //判断容器是否为空 swap(st); //交换两个集合容器 #include iostream using namespace std; #includeset //set容器大小和交换 //size(); //返回容器中元素的数目 //empty(); //判断容器是否为空 //swap(st); //交换两个集合容器 void printSet(setint s) {for (setint::const_iterator it s.begin(); it ! s.end(); it){cout *it ;}cout endl; }void test01() {//创建set容器setints1;//默认构造//插入数据只有insert方式s1.insert(10);s1.insert(40);s1.insert(30);s1.insert(20);s1.insert(30);//遍历容器//set容器特点所有元素插入时自动被排序//set容器不允许插入重复值printSet(s1);//判断是否为空if (s1.empty()){cout s1为空 endl;}else{cout s1不为空 endl;cout s1的大小为 s1.size() endl;} }void test02() {setints1;//插入数据只有insert方式s1.insert(10);s1.insert(30);s1.insert(20);s1.insert(40);setints2;//插入数据只有insert方式s2.insert(100);s2.insert(300);s2.insert(200);s2.insert(400);cout 交换前 endl;printSet(s1);printSet(s2);//交换s1.swap(s2);cout 交换后 endl;printSet(s1);printSet(s2); }int main() {test01();test02();system(pause);return 0; } 输出结果 10 20 30 40 s1不为空 s1的大小为4 交换前 10 20 30 40 100 200 300 400 交换后 100 200 300 400 10 20 30 40 请按任意键继续. . . 1.4set插入和删除 功能描述set容器进行插入数据和删除数据 函数原型: insert(elem); //在容器中插入元素 clear(); //清除所有元素 erase(pos); //删除pos迭代器所指的元素返回下一个元素的迭代器 erase(beg, end); //删除区间[beg,end)的所有元素 返回下一个元素的迭代器 erase(elem); //删除容器中值为elem的元素 #include iostream using namespace std; #includeset //set容器插入数据和删除数据 //insert(elem); //在容器中插入元素 //clear(); //清除所有元素 //erase(pos); //删除pos迭代器所指的元素返回下一个元素的迭代器 //erase(beg, end); //删除区间[beg,end)的所有元素 返回下一个元素的迭代器 //erase(elem); //删除容器中值为elem的元素 void printSet(setint s) {for (setint::const_iterator it s.begin(); it ! s.end(); it){cout *it ;}cout endl; }void test01() {//创建set容器setints1;//默认构造//插入数据只有insert方式s1.insert(30);s1.insert(10);s1.insert(20);s1.insert(40);//遍历容器//set容器特点所有元素插入时自动被排序//set容器不允许插入重复值printSet(s1);//删除s1.erase(s1.begin());//因为排序了所以是把10删除了printSet(s1);//删除重载版本s1.erase(30);printSet(s1);//清空//二者实现功能一致//s1.erase(s1.begin(), s1.end());s1.clear();printSet(s1); }int main() {test01();system(pause);return 0; } 输出结果 10 20 30 40 20 30 40 20 40请按任意键继续. . . 1.5set查找和统计 功能描述对set容器进行查找数据以及统计数据 函数原型: find(key); //查找key是否存在,若存在返回该键的元素的选代器;若不存在返回set.end(); count(key);//统计key的元素个数 注意 set.end()是最后一个元素的下一位置可以理解为空 对于set而言count()的统计结果要么是0要么是1。因为set容器不允许插入重复值 #include iostream using namespace std; #includeset //set容器查找和统计 //find(key); //查找key是否存在,若存在返回该键的元素的选代器;若不存在返回set.end(); //count(key);//统计key的元素个数void test01() {//创建set容器setints1;//默认构造//插入数据只有insert方式s1.insert(30);s1.insert(10);s1.insert(20);s1.insert(40);s1.insert(30);s1.insert(30);//遍历容器//set容器特点所有元素插入时自动被排序//set容器不允许插入重复值setint::iterator pos s1.find(40);if (pos s1.end()){cout 未找到元素 endl;}else{cout 找到元素: *pos endl;}//统计30的个数//对于set而言统计结果要么是0要么是1int num s1.count(30);cout num num endl; }int main() {test01();system(pause);return 0; } 输出结果 找到元素:40 num 1 请按任意键继续. . . 1.6set和multiset区别 学习目标掌握set和multiset的区别 区别 set不可以插入重复数据而multiset可以 set插入数据的同时会返回插入结果表示插入是否成功返回的结果会以对组显示 multiset不会检测数据因此可以插入重复数据 总结 如果不允许插入重复数据可以利用set 如果需要插入重复数据利用multiset #include iostream using namespace std; #includeset //set和multiset区别 void printSet(setint s) {for (setint::const_iterator it s.begin(); it ! s.end(); it){cout *it ;}cout endl; }void test01() {//创建set容器setints1;//默认构造//插入数据只有insert方式//返回值的对组我们尝试接收一下//第一个值是个迭代器 第二个值是boolpair setint::iterator, bool ret s1.insert(10);if (ret.second){cout 第一次插入为真 endl;}else{cout 第一次插入为假 endl;}//set容器不允许插入重复值ret s1.insert(10);if (ret.second){cout 第二次插入为真 endl;}else{cout 第二次插入为假 endl;}multisetintms;//允许插入重复值ms.insert(10);ms.insert(10);ms.insert(10);//遍历容器for (multisetint::iterator it ms.begin(); it ! ms.end(); it){cout *it ;}cout endl; }int main() {test01();system(pause);return 0; } 输出结果 第一次插入为真 第二次插入为假 10 10 10 请按任意键继续. . . 1.7pair对组创建 功能描述成对出现的数据利用对组可以返回两个数据 两种创建方式 pairtype, typep(value1, value2); pairtype, typep make_pair(value1, value2); #include iostream using namespace std; #includestring //pair对组创建 //两种创建方式 : //pairtype, typep(value1, value2); //pairtype, typep make_pair(value1, value2); void test01() {//第一种方式pairstring, intp(Tom, 20);cout 姓名 p.first 年龄 p.second endl;//第二种方式pairstring, intp2(jerry, 30);cout 姓名 p2.first 年龄 p2.second endl; }int main() {test01();system(pause);return 0; } 输出结果 姓名Tom年龄20 姓名jerry年龄30 请按任意键继续. . . 1.8set容器排序 学习目标         set容器默认排序规则为从小到大掌握如何改变排序规则 主要技术点         利用仿函数可以改变排序规则 #include iostream using namespace std; #includeset //set容器排序内置数据类型 class myCompare { public:bool operator()(int v1, int v2)const{return v1 v2;} };void test01() {setints1;s1.insert(10);s1.insert(40);s1.insert(20);s1.insert(50);s1.insert(30);for (setint::iterator it s1.begin(); it ! s1.end(); it){cout *it ;}cout endl;//指定排序规则为从大到小setint, myCompares2;s2.insert(10);s2.insert(40);s2.insert(20);s2.insert(50);s2.insert(30);for (setint, myCompare::iterator it s2.begin(); it ! s2.end(); it){cout *it ;}cout endl; }int main() {test01();system(pause);return 0; } 输出结果 10 20 30 40 50 50 40 30 20 10 请按任意键继续. . . #include iostream using namespace std; #includestring #includeset //set容器排序存放自定义数据类型 class Person { public:Person(string name, int age){this-m_Name name;this-m_Age age;}string m_Name;int m_Age; };class myComparePerson { public:bool operator()(const Person p1, const Person p2)const{//按照年龄降序return p1.m_Age p2.m_Age;} };void test01() {//自定义数据类型都会先指定排序规则setPerson, myComparePersons;//创建Person对象Person p1(刘备, 24);Person p2(关羽, 28);Person p3(张飞, 25);Person p4(赵云, 21);s.insert(p1);s.insert(p2);s.insert(p3);s.insert(p4);for (setPerson, myComparePerson::const_iterator it s.begin(); it ! s.end(); it){cout 姓名 it-m_Name 年龄 it-m_Age endl;} }int main() {test01();system(pause);return 0; } 输出结果 姓名关羽年龄28 姓名张飞年龄25 姓名刘备年龄24 姓名赵云年龄21 请按任意键继续. . . 2、map / multimap容器 2.1map基本概念 简介 map中所有元素都是pair对组 pair中第一个元素为key(键值)起到索引作用第二个元素为value(实值) 所有元素都会根据元素的键值自动排序 本质 map/multimap属于关联式容器底层结构是用二叉树实现 优点 可以根据key值快速找到value值 map和multimap区别 map不允许容器中有重复key值元素 multimap允许容器中有重复key值元素 2.2map构造和赋值 功能描述对map容器进行构造和赋值操作 函数原型 //构造 mapT1T2 mp; //map默认构造函数 map(const map mp);//拷贝构造函数 //赋值: map operator(const map mp);//重载等号操作符 #include iostream using namespace std; #includemap //map容器构造和赋值 //构造: //mapT1T2 mp; //map默认构造函数 //map(const map mp); //拷贝构造函数 //赋值: //map operator(const map mp);//重载等号操作符 void printMap(mapint, int m) {for (mapint, int::iterator it m.begin(); it ! m.end(); it){cout key (*it).first value it-second endl;}cout endl; }void test01() {//创建map容器mapint, intm;m.insert(pairint, int(4, 40));m.insert(pairint, int(3, 30));m.insert(pairint, int(2, 20));m.insert(pairint, int(1, 10));printMap(m);//拷贝构造mapint, intm2(m);printMap(m2);//赋值mapint, intm3;m3 m2;printMap(m3); }int main() {test01();system(pause);return 0; } 总结map中所有元素都是成对出现的插入数据时要使用对组  输出结果 key 1 value 10 key 2 value 20 key 3 value 30 key 4 value 40key 1 value 10 key 2 value 20 key 3 value 30 key 4 value 40key 1 value 10 key 2 value 20 key 3 value 30 key 4 value 40请按任意键继续. . . 2.3map大小和交换 功能描述统计map容器大小以及交换map容器 函数原型 size(); //返回容器中元素的数目 empty(); //判断容器是否为空 swap(st); //交换两个集合容器 #include iostream using namespace std; #includemap //map容器大小和交换 //size(); //返回容器中元素的数目 //empty(); //判断容器是否为空 //swap(st); //交换两个集合容器 void printMap(mapint, int m) {for (mapint, int::iterator it m.begin(); it ! m.end(); it){cout key (*it).first value it-second endl;}cout endl; } //大小 void test01() {//创建map容器mapint, intm; m.insert(pairint, int(1, 10));m.insert(pairint, int(2, 20));m.insert(pairint, int(3, 30));if (m.empty()){cout m为空 endl;}else{cout m不为空 endl;cout m的大小为 m.size() endl;} } //交换 void test02() {mapint, intm;m.insert(pairint, int(1, 10));m.insert(pairint, int(2, 20));m.insert(pairint, int(3, 30));mapint, intm2;m2.insert(pairint, int(4, 100));m2.insert(pairint, int(5, 200));m2.insert(pairint, int(6, 300));cout 交换前 endl;printMap(m);printMap(m2);m.swap(m2);cout 交换后 endl;printMap(m);printMap(m2); }int main() {test01();test02();system(pause);return 0; } 输出结果 m不为空 m的大小为3 交换前 key 1 value 10 key 2 value 20 key 3 value 30key 4 value 100 key 5 value 200 key 6 value 300交换后 key 4 value 100 key 5 value 200 key 6 value 300key 1 value 10 key 2 value 20 key 3 value 30请按任意键继续. . . 2.4map插入和删除 功能描述map容器进行插入数据和删除数据 函数原型 insert(elem); //在容器中插入元素 clear(); //清除所有元素 erase(pos); //删除pos迭代器所指的元素返回下一个元素的迭代器 erase(beg, end); //删除区间[beg,end)的所有元素返回下一个元素的迭代器 erase(key); //删除容器中值为key的元素 #include iostream using namespace std; #includemap //map容器插入数据和删除数据 //insert(elem); //在容器中插入元素 //clear(); //清除所有元素 //erase(pos); //删除pos迭代器所指的元素返回下一个元素的迭代器 //erase(beg, end); //删除区间[beg,end)的所有元素返回下一个元素的迭代器 //erase(key); //删除容器中值为key的元素 void printMap(mapint, int m) {for (mapint, int::iterator it m.begin(); it ! m.end(); it){cout key (*it).first value it-second endl;}cout endl; }void test01() {//创建map容器mapint, intm; //插入//第一种m.insert(pairint, int(1, 10));//第二种m.insert(make_pair(2, 20));//第三种m.insert(mapint, int::value_type(3, 30));//第四种m[4] 40;//[]不建议插入用途 可以利用key访问到valuecout m[4] endl;printMap(m);//删除m.erase(m.begin());printMap(m);m.erase(3);//按照key删除printMap(m);//清空m.erase(m.begin(), m.end());//m.clear(); //二者等价printMap(m); }int main() {test01();system(pause);return 0; } 输出结果 40 key 1 value 10 key 2 value 20 key 3 value 30 key 4 value 40key 2 value 20 key 3 value 30 key 4 value 40key 2 value 20 key 4 value 40请按任意键继续. . . 2.5map查找和统计 功能描述对map容器进行查找数据以及统计数据 函数原型 find(key); //查找key是否存在,若存在返回该键的元素的迭代器;若不存在返回set,end(); count(key);//统计key的元素个数 #include iostream using namespace std; #includemap //map容器查找数据以及统计数据 //find(key); //查找key是否存在,若存在返回该键的元素的迭代器;若不存在返回set,end(); //count(key);//统计key的元素个数 void test01() {//查找//创建map容器mapint, intm; //插入数据m.insert(pairint, int(1, 10));m.insert(pairint, int(2, 20));m.insert(pairint, int(3, 30));mapint, int::iterator pos m.find(3);//返回值为迭代器if (pos ! m.end()){cout 查到了元素 key (*pos).first value pos-second endl;}else{cout 未找到元素 endl;}//统计int num m.count(3);//map不允许插入重复key元素count统计值只能为0或1//multimap的count值可能大于1cout num num endl; }int main() {test01();system(pause);return 0; } 输出结果 查到了元素 key 3 value 30 num 1 请按任意键继续. . . 2.6map容器排序 学习目标map容器默认排序规则为 按照key值进行 从小到大排序掌握如何改变排序规则 主要技术点利用仿函数可以改变排序规则 #include iostream using namespace std; #includemap //map容器排序 void test01() {//创建map容器mapint, intm; //插入数据m.insert(pairint, int(1, 10));m.insert(pairint, int(5, 50));m.insert(pairint, int(3, 30));m.insert(pairint, int(4, 40));m.insert(pairint, int(2, 20));for (mapint, int::iterator it m.begin(); it ! m.end(); it){cout key (*it).first value it-second endl;}cout endl; }class MyCompare { public:bool operator()(int v1, int v2)const{//降序return v1 v2;} };void test02() {//创建map容器mapint, int, MyComparem1;//插入数据m1.insert(pairint, int(1, 10));m1.insert(pairint, int(5, 50));m1.insert(pairint, int(3, 30));m1.insert(pairint, int(4, 40));m1.insert(pairint, int(2, 20));for (mapint, int, MyCompare::iterator it m1.begin(); it ! m1.end(); it){cout key (*it).first value it-second endl;}cout endl; }int main() {test01();test02();system(pause);return 0; } 输出结果 key 1 value 10 key 2 value 20 key 3 value 30 key 4 value 40 key 5 value 50key 5 value 50 key 4 value 40 key 3 value 30 key 2 value 20 key 1 value 10请按任意键继续. . . 3、STL案例---员工分组 案例描述 公司今天招聘了10个员工(ABCDEFGHI)10名员工进入公司之后需要指派员工在那个部门工作。员工信息有: 姓名 工资组成部门分为策划、美术、研发。随机给10名员工分配部门和工资。通过multimap进行信息的插入 key(部门编号) value(员工)。分部门显示员工信息 实现步骤 1.创建10名员工放到vector中 2.遍历vector容器取出每个员工进行随机分组 3.分组后将员工部门编号作为key具体员工作为value放入到multimap容器中 4.分部门显示员工信息 #include iostream using namespace std; #includevector #includestring #includemap #includectime#define CEHUA 0 #define MEISHU 1 #define YANFA 2class Worker { public:string m_Name;int m_Salary; };void creatWorker(vectorWorker v) {string nameSeed ABCDEFGHIJ;for (int i 0; i 10; i){Worker worker;worker.m_Name 员工;worker.m_Name nameSeed[i];worker.m_Salary rand() % 10000 10000;//10000--19999//将员工放入到容器中v.push_back(worker);} } //员工分组 void setGroup(vectorWorker v, multimapint, Worker m) {for (vectorWorker::iterator it v.begin(); it ! v.end(); it){//产生随机部门编号int deptID rand() % 3;//0 1 2//将员工插入到分组中//key部门编号value具体员工m.insert(make_pair(deptID, *it));} }void showWorkerByGroup(multimapint, Worker m) {//0 1 2cout 策划部门 endl;multimapint, Worker::iterator pos m.find(CEHUA);int count m.count(CEHUA);//统计策划部门的人数int index 0;for (; pos ! m.end() index count; pos, index){cout 姓名 pos-second.m_Name 工资 pos-second.m_Salary endl;}cout ----------------------------- endl;cout 美术部门 endl;pos m.find(MEISHU);count m.count(MEISHU);//统计美术部门的人数index 0;for (; pos ! m.end() index count; pos, index){cout 姓名 pos-second.m_Name 工资 pos-second.m_Salary endl;}cout ----------------------------- endl;cout 研发部门 endl;pos m.find(YANFA);count m.count(YANFA);//统计美术部门的人数index 0;for (; pos ! m.end() index count; pos, index){cout 姓名 pos-second.m_Name 工资 pos-second.m_Salary endl;} } int main() {srand((unsigned int)time(NULL));//随机数种子//1、创建员工vectorWorkervWorker;creatWorker(vWorker);//测试/*for (vectorWorker::iterator it vWorker.begin(); it ! vWorker.end(); it){cout 姓名 it-m_Name 工资 it-m_Salary endl;}*///2、员工分组multimapint, WorkermWorker;setGroup(vWorker, mWorker);//3、分组显示员工showWorkerByGroup(mWorker);system(pause);return 0; } 输出结果因为加入了随机数种子每次结果都不一样 策划部门 姓名员工B工资12401 姓名员工G工资12710 姓名员工H工资11054 姓名员工I工资17756 姓名员工J工资13103 ----------------------------- 美术部门 姓名员工A工资12620 姓名员工C工资17687 姓名员工D工资16582 姓名员工F工资12674 ----------------------------- 研发部门 姓名员工E工资16102 请按任意键继续. . .
http://www.zqtcl.cn/news/265599/

相关文章:

  • 国外做项目的网站软件定制外包平台
  • 做网站要用什么软件房地产建设网站
  • 龙岗爱联有学网站建设装饰公司简介
  • pc端网站怎么做自适应哪个公司网站备案快
  • 品牌网站建设黑白I狼J烟台开发区建设业联合网站
  • 做视频网站可以自学吗php html5企业网站源码
  • 阿里云怎么部署网站引流推广平台是什么意思
  • 江山建设工程信息网站营销网讯
  • 网站制作公司 沈阳上海建设主管部门网站
  • 网站建设前期如何做好市场定位分析网络推广主要工作内容
  • 做一个网站的流程是什么金融网站建设方案
  • 汽车维修保养网站模板北京网站建设知名公司排名
  • 网站建设案例分享网络推广网
  • 广州知名网站推广app软件开发制作公司电话
  • 泉州专业网站建设seo是指什么职位
  • 怎么做房产网站张家港高端网站制作
  • 做网站运营公司收费广东短视频seo搜索哪家好
  • 外贸网站 源码做的好详情页网站
  • 冀州网站制作邢台百姓网官网
  • 佛山做外贸网站方案自助网站推广系统
  • 安徽鸿顺鑫城建设集团网站小区物业管理网站开发报告
  • 有关网站建设文章常熟做网站多少钱
  • 网站流量报表江苏住房和城乡建设厅网站
  • 提供做网站公司有哪些个人建网站的费用
  • 网站后台添加表格wordpress垂直分页导航插件
  • 重庆网站建设有限公司六安市裕安区建设局网站
  • 北京产品网站建设如何做移动支付网站
  • 做同城购物网站赚钱吗设计企业网站流程
  • 网站要用什么软件做建设工程施工合同专属管辖
  • 模板网站建设制作佛山正规网站建设哪家好