首页 > 代码库 > 第14章 multimap多重映照容器
第14章 multimap多重映照容器
/* 第14章 multimap多重映照容器 14.1 multimap技术原理 14.2 multimap应用基础 14.3 本章小结*/// 第14章 multimap多重映照容器// 14.1 multimap技术原理 ------------------------------------------------------------------------------// 14.2 multimap应用基础 ------------------------------------------------------------------------------//212 插入、遍历#include <map>#include <iostream>int main(void){ using namespace std; multimap < float, char * > mm; mm.insert(pair < float, char * > (3.0f, "apple")); mm.insert(pair < float, char * > (3.0f, "pear")); mm.insert(pair < float, char * > (2.6f, "orange")); mm.insert(pair < float, char * > (1.8f, "banana")); mm.insert(pair < float, char * > (6.3f, "lichee")); //遍历打印 multimap < float, char * > ::iterator i, iend; iend = mm.end(); for(i = mm.begin(); i != iend; i++) cout << (*i).second << ‘ ‘ << (*i).first << "元/斤\n"; cout << endl; return 0;}//213 反向遍历#include <map>#include <iostream>int main(void){ using namespace std; multimap < float, char * > mm; mm.insert(pair < float, char * > (3.0f, "apple")); mm.insert(pair < float, char * > (3.0f, "pear")); mm.insert(pair < float, char * > (2.6f, "orange")); mm.insert(pair < float, char * > (1.8f, "banana")); mm.insert(pair < float, char * > (6.3f, "lichee")); //反向遍历打印 multimap < float, char * > ::reverse_iterator r_i, r_iend; r_iend = mm.rend(); for(r_i = mm.rbegin(); r_i != r_iend; r_i++) cout << (*r_i).second << ‘ ‘ << (*r_i).first << "元/斤\n"; return 0;}//214#include <map>#include <iostream>struct CourseRecord{ //课程记录结构体 struct CourseInfo { //课程信息结构体 char *course; //课程名 int period; //学时 char *required; //必修或选修 }; char *teacher; //任课教师 CourseInfo cf; //课程信息 CourseRecord(char *teacher_, char *course_, int period_, char *required_) { teacher = teacher_; cf.course = course_; cf.period = period_; cf.required = required_; }};int main(void){ using namespace std; //创建multimap容器对象mm typedef multimap < char *, CourseRecord::CourseInfo > coursemmap; coursemmap mm; //插入第1条记录 CourseRecord course1 = CourseRecord("王强", "操作系统开发", 60, "必修"); pair < char *, CourseRecord::CourseInfo > pairCourse1(course1.teacher, course1.cf); mm.insert(pairCourse1); //插入第2条记录 CourseRecord course2 = CourseRecord("李文", "编译器开发", 30, "必修"); pair < char *, CourseRecord::CourseInfo > pairCourse2(course2.teacher, course2.cf); mm.insert(pairCourse2); //插入第3条记录 CourseRecord course3 = CourseRecord("李文", "数据结构", 20, "必修"); pair < char *, CourseRecord::CourseInfo > pairCourse3(course3.teacher, course3.cf); mm.insert(pairCourse3); //插入第4条记录 CourseRecord course4 = CourseRecord("李文", "Java开发应用", 38, "选修"); pair < char *, CourseRecord::CourseInfo > pairCourse4(course4.teacher, course4.cf); mm.insert(pairCourse4); //插入第5条记录 CourseRecord course5 = CourseRecord("张键", "pascal语言", 26, "选修"); pair < char *, CourseRecord::CourseInfo > pairCourse5(course5.teacher, course5.cf); mm.insert(pairCourse5); //记录搜索 cout << "搜索<李文老师>的任课记录:\n"; pair < coursemmap::iterator, coursemmap::iterator > p = mm.equal_range("李文") ; //打印 coursemmap::iterator i; for(i = p.first; i != p.second; i++) cout << (*i).first << ‘ ‘ << (*i).second.course << ‘ ‘ << (*i) .second.period << "学时 " << (*i).second.required << ‘ ‘ << endl; cout << endl << endl; return 0;}//215#include <map>#include <iostream>int main(void){ using namespace std; multimap < int, char > mm; cout << mm.size() << endl; //打印:0 mm.insert(pair < int, char > (3, ‘a‘)); mm.insert(pair < int, char > (3, ‘c‘)); mm.insert(pair < int, char > (6, ‘f‘)); cout << mm.count(3) << endl; //打印:2 cout << mm.size() << endl; //打印:3 return 0;}// 14.3 本章小结 ------------------------------------------------------------------------------
TOP
第14章 multimap多重映照容器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。