1.map中数据的遍历
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
map <int, string> mymap;
mymap[1] = "student 1";
mymap[2] = "student 2";
mymap[3] = "student 3";
//数组方式
int n = mymap.size();
for(int i = 0; i < n; i++)
cout << mymap[i+1] <<endl;
//前向迭代器
map <int, string>::iterator it;
for(it = mymap.begin(); it != mymap.end(); it++)
cout << it->second << endl;
//反向迭代器
map <int, string>::reverse_iterator iter;
for(iter = mymap.rbegin(); iter != mymap.rend(); iter++)
cout << iter->second << endl;
return 0;
}
2.数据的查找。
用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
int main()
{
map <int, string> mymap;
mymap[1] = "student 1";
mymap[2] = "student 2";
mymap[3] = "student 3";
map <int, string>::iterator it;
it = mymap.find(1);
if(it != mymap.end())
cout << it->second << endl;
else cout << "oh!" << endl;
return 0;
}
Lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
Upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,而upper-bound(2)的话,返回的就是3
Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字
int main()
{
map <int, string> mymap;
mymap[1] = "student 1";
mymap[3] = "student 3";
mymap[5] = "student 5";
map <int, string>::iterator it;
it = mymap.lower_bound(2);
cout << it->second << endl; // 2的上界 输出"student 3"
it = mymap.lower_bound(3);
cout << it->second << endl; // 3的上界 输出"student 3"
it = mymap.upper_bound(2);
cout << it->second << endl; // 2的上界 输出"student 3"
it = mymap.upper_bound(3);
cout << it->second << endl; // 3的上界 输出"student 5"
pair < map<int,string>::iterator, map<int,string>::iterator >mypair;
mypair = mymap.equal_range(2);//查找2,输出"Not Find !"
if(mypair.first == mypair.second)
cout << "Not Find !" << endl;
else cout << "Find !" << endl;
mypair = mymap.equal_range(3);//查找3,输出"Find !"
if(mypair.first == mypair.second)
cout << "Not Find !" << endl;
else cout << "Find !" << endl;
return 0;
}
3. 数据清空 clear()
判空 empty() 为空返回true,否则返回false
4.数据删除:包括用迭代器删除,用关键字删除,成片删除
int main()
{
map <int, string> mymap;
mymap[1] = "student 1";
mymap[3] = "student 3";
mymap[5] = "student 5";
map <int, string>::iterator it;
it = mymap.find(1); // 删除1, 用迭代器删除
mymap.erase(it);
int t = mymap.erase(1); // 删除1,用关键字删除,若删除成功,返回1.
if(t == 1)
cout << "YES!" << endl;
mymap.erase(mymap.begin(), mymap.end()); // 成片删除,删除区间是一个前闭后开的集合
/*for(it = mymap.begin(); it != mymap.end(); it++)
cout << it->second << endl;*/
return 0;
}
5. 排序
因为上面map中的关键字是int,支持 小于号。但关键字变成结构体,就不能直接插入,应重载一个小于号。
typedef struct info
{
int ID;
string name;
bool operator < (const info & tmp)const // 重载 小于号
{
if(tmp.ID == ID) //ID 相同, 按名字从小到大
return name < tmp.name;
return ID < tmp.ID;
}
}StuInfo;
map <StuInfo, int> mymap;
int main()
{
StuInfo t;
t.ID = 1;
t.name = "student1";
mymap[t] = 90;
t.ID = 2;
t.name = "student2";
mymap[t] = 80;
map <StuInfo, int>:: iterator it;
for(it = mymap.begin(); it != mymap.end(); it++)
cout << it->second << endl;
return 0;
}