数据结构 map的学习
2014-07-25 15:48
106 查看
// // main.cpp // map使用 // // Created by 刘鑫伟 on 14-7-25. // Copyright (c) 2014年 刘鑫伟. All rights reserved. // #include<map> #include<stdio.h> #include<iostream> using namespace std; int main() { map<string, int> months; //定义 months["january"] = 31; //插入值 前面是key值 后面是对应得附加值 months["february"] = 28; cout << "june -> " << months["june"] << endl; //输出时候通过访问数组形式 即可输出对应值 map<string ,int>::iterator cur = months.find("june"); //寄存器 查找元素 map<string, int>::iterator prev = cur; map<string, int>::iterator next = cur; ++next; --prev; cout << "Previous (in alphabetical order) is " << (*prev).first << endl; cout << "Next (in alphabetical order) is " << (*next).first << endl; } //清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map //如果要删除1,用迭代器删除 //map<int, string>::iterator iter; //iter = mapStudent.find(1); //mapStudent.erase(iter); //如果要删除1,用关键字删除 //int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0 //用迭代器,成片的删除 //一下代码把整个map清空 //mapStudent.earse(mapStudent.begin(), mapStudent.end()); //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合 //map里得元素按关键值会排序 越小越靠前 如果想定义优先级高得map需要重载运算符 /* #include <map> #include <string> using namespace std; typedef struct tagStudentInfo { Int nID; String strName; }studentInfo, *PStudentInfo; //学生信息 classs sort { public: bool operator() (StudentInfo const &_A, StudentInfo const &_B) const { If(_A.nID < _B.nID) return true; If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0; Return false; } }; int main() { //用学生信息映射分数 map<StudentInfo, int, sort>mapStudent; studentInfo studentInfo; studentInfo.nID = 1; studentInfo.strName = “student_one”; mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90)); studentInfo.nID = 2; studentInfo.strName = “student_two”; mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80)); } */
相关文章推荐
- ECMAScript 6 学习系列课程 (ES6 Set和Map数据结构)
- ECMAScript 6 学习笔记----set和map数据结构
- 【学习笔记】Bit-map数据结构
- Java数据结构之Map学习总结
- es6学习笔记8--Map数据结构
- sizzle.js学习笔记利用闭包模拟实现数据结构:字典(Map)
- Java数据结构之Map学习总结
- 数据结构学习-递归初接触
- 数据结构学习之 第一章 绪论
- 数据结构学习-递归与分治
- 数据结构学习(2):汉诺塔问题
- [导入]队列(C#数据结构学习三)
- 数据结构学习笔记之一:链表
- 数据结构学习(5):链表
- 数据结构学习-递归与动态编程
- 如何学习数据结构--致刚上大二的学弟学妹
- 累啊!!!终于完成了工作计划,可以继续我的数据结构的学习了!!!!
- 一个准备学习数据结构学生的困惑
- 数据结构学习—二叉树
- 数据结构学习笔记