C++,stl,map/multimap详解

目录

1.map的构造和赋值 

2.map的大小和交换

3.map的插入和删除

4.map的查找和统计

5.map的排序


1.map的构造和赋值 

#include<bits/stdc++.h>
using namespace std;

void print(map<int,int> &mp)
{
	for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << it -> first << ' ' << it -> second;
		cout << endl;
	}
	cout << endl;
}

int main()
{
	map<int,int> mp;
	mp.insert(pair<int,int>(1,90));
	mp.insert(pair<int,int>(2,950));
	mp.insert(pair<int,int>(7,920));
	mp.insert(pair<int,int>(5,10));
	mp.insert(pair<int,int>(6,80));
	//按照key的值进行排序
	print(mp);
	
	return 0;
}

 

2.map的大小和交换

#include<bits/stdc++.h>
using namespace std;

void print(map<int,int> &mp)
{
	for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << it -> first << ' ' << it -> second;
		cout << endl;
	}
	cout << endl;
}

int main()
{
	map<int,int> mp;
	mp.insert(pair<int,int>(1,90));
	mp.insert(pair<int,int>(2,950));
	mp.insert(pair<int,int>(7,920));
	mp.insert(pair<int,int>(5,10));
	mp.insert(pair<int,int>(6,80));
	//按照key的值进行排序
	print(mp);
	
	cout << mp.empty() << endl;
	cout << mp.size() << endl;
	
	map<int,int>mp1;
	mp1.swap(mp);
	print(mp1);
	
	return 0;
}

3.map的插入和删除

#include<bits/stdc++.h>
using namespace std;

void print(map<int,int> &mp)
{
	for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << it -> first << ' ' << it -> second;
		cout << endl;
	}
	cout << endl;
}

int main()
{
	map<int,int> mp;
	mp.insert(pair<int,int>(1,90));
	mp.insert(pair<int,int>(2,950));
	
	mp.insert(make_pair(7,920));
	
	mp.insert(pair<int,int>(5,10));
	
	mp[6] = 80;
	//按照key的值进行排序
	print(mp);
	
	mp.erase(6);
	//按照key删除
	mp.erase(mp.begin());
	//也可以传迭代器进行删除
	print(mp);
	
	mp.clear();
	print(mp);
	
	return 0;
}

4.map的查找和统计

#include<bits/stdc++.h>
using namespace std;

void print(map<int,int> &mp)
{
	for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << it -> first << ' ' << it -> second;
		cout << endl;
	}
	cout << endl;
}

int main()
{
	map<int,int> mp;
	mp.insert(pair<int,int>(1,90));
	mp.insert(pair<int,int>(2,950));
	mp.insert(make_pair(7,920));
	mp.insert(pair<int,int>(5,10));
	mp[6] = 80;
	
	map<int,int>::iterator it = mp.find(5);
	//从前往后找map里有没有key为5的,没找到的话迭代器就在mp.end()处
	if(it != mp.end()) cout << "找到了" << endl;
	
	cout << mp.count(7) << endl;
	//输出有几个key为7的数,这里只有一个
	
	return 0;
}

5.map的排序

改成从大到小 

#include<bits/stdc++.h>
using namespace std;

class cmp
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};

int main()
{
	map<int,int,cmp> mp;
	mp.insert(pair<int,int>(1,90));
	mp.insert(pair<int,int>(2,950));
	mp.insert(make_pair(7,920));
	mp.insert(pair<int,int>(5,10));
	mp[6] = 80;
	
	for(map<int,int,cmp>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << it -> first << ' ' << it -> second << endl;
	}
	
	return 0;
}

 

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏箱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值