初识STL之map容器

1.map基本概念

简介:

map中的所有元素都是pair
pair中的第一个元素为key,起到索引作用,第二代元素为value
所有元素会根据元素的key自动排序

本质:
map/multimap属于关联式容器,底层结构式用二叉树实现的.

map和multimap区别:

map不允许容器里面有重复的key元素
multimap允许容器里面有重复的key元素

2.map构造和赋值

构造:

map<T1,T2>mp;
map(const map& map);

赋值:

map& operator=(const map& mp);
#include<iostream>
#include<map>

using namespace std;

void PrintMap(map<int,int>& m)
{
	for(auto m1:m)
		cout<<m1.first<<" "<<m1.second<<endl;
	cout<<endl;
}

void text()
{
	map<int,int>m;
	
	m.insert(pair<int,int>(1,10));	
	m.insert(make_pair(2,20));	
	PrintMap(m);
	
	map<int,int>m1(m);
	PrintMap(m1);
	
	map<int,int>m2;
	m2=m1;
	PrintMap(m2);
}

int main()
{
	text();
}

3.map大小和交换

函数原型:

size();
empty();
swap(mp);
#include<iostream>
#include<map>

using namespace std;

void PrintMap(map<int,int>& m)
{
	for(auto m1:m)
		cout<<m1.first<<" "<<m1.second<<endl;
	cout<<endl;
}

void text()
{
	map<int,int>m;
	
	m.insert(pair<int,int>(1,10));	
	m.insert(make_pair(2,20));	
	
	
	int Size =  m.size();
	//2
	cout<<Size<<endl;
	
	if(!m.empty()) PrintMap(m);
	

}

int main()
{
	text();
}

4.map的插入和删除

函数原型:

insert(elem);
clear();
erase(pos);  //删除pos迭代器所指向的元素,返回下一个元素的迭代器
erase(beg,end); //删除区间[beg,end)所有的元素,返回下一个元素的迭代器
erase(elem);//删除容器中elem元素;
#include<iostream>
#include<map>

using namespace std;

void PrintMap(map<int,int>& m)
{
	for(auto m1:m)
		cout<<m1.first<<" "<<m1.second<<endl;
	cout<<endl;
}

void text()
{
	map<int,int>m;
	
	m.insert(pair<int,int>(1,10));	
	m.insert(make_pair(2,20));	
	m.insert(pair<int,int>(3,30));	
	m.insert(make_pair(4,40));
		
	map<int,int>::iterator m_begin = m.begin();
	map<int,int>::iterator m_end = m.end();
	
	m_begin++;
	m_begin++;
	m_end--;
	
	m.erase(m_begin,m_end);
	/*
	1 10
	2 20
	4 40
	*/
	
	PrintMap(m);
	

}

int main()
{
	text();
}
#include<iostream>
#include<map>

using namespace std;

void PrintMap(map<int,int>& m)
{
	for(auto m1:m)
		cout<<m1.first<<" "<<m1.second<<endl;
	cout<<endl;
}

void text()
{
	map<int,int>m;
	
	m.insert(pair<int,int>(1,10));	
	m.insert(make_pair(2,20));	
	m.insert(pair<int,int>(3,30));	
	m.insert(make_pair(4,40));
	
	m.erase(m.begin());
	/*
	2 20
	3 30
	4 40
	*/
	PrintMap(m);
	m.erase(4);
	PrintMap(m);
	/*
	2 20
	3 30
	*/

	m.erase(m.begin(),m.end());
	m.clear();
	//empty
	

}

int main()
{
	text();
}


5.map查找和统计

函数原型:

find(key); //查找key是否存在,返回该键的迭代器,若不存在返回set.end();
count(key);//统计key元素个数
#include<iostream>
#include<map>

using namespace std;

void PrintMap(map<int,int>& m)
{
	for(auto m1:m)
		cout<<m1.first<<" "<<m1.second<<endl;
	cout<<endl;
}

void text()
{
	map<int,int>m;
	
	m.insert(pair<int,int>(1,10));	
	m.insert(make_pair(2,20));	
	m.insert(pair<int,int>(3,30));	
	m.insert(make_pair(4,40));
		
	map<int,int>::iterator pos = m.find(2);
	if(pos!=m.end()) cout<<(*pos).first<<" "<<(*pos).second<<endl;

	
	//PrintMap(m);
	

}

int main()
{
	text();
}

6.map容器排序

set容器默认排序为从小到大,利用仿函数,可以改变排序规则.

#include<iostream>
#include<map>
#include<cstring>
using namespace std;

struct MyCompare
{

	bool operator() (int v1,int v2)
	{
		return v1>v2;
	} 
};

void PrintMap(map<int,int,MyCompare>& m)
{
	for(auto m1:m)
		cout<<m1.first<<" "<<m1.second<<endl;
	cout<<endl;
}

void text()
{
	map<int,int,MyCompare>m;
	
	m.insert(pair<int,int>(1,10));	
	m.insert(make_pair(2,20));	
	m.insert(pair<int,int>(3,30));	
	m.insert(make_pair(4,40));
	
	PrintMap(m);
	
}


int main()
{
	text();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值