//
// 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));
}
*/