STL—map


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中已经插入了1234的话,如果lower_bound(2)的话,返回的2,而upper-bound2)的话,返回的就是


Equal_range
函数返回一个pairpair里面第一个变量是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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值