map的赋值,交换以及初始化

#include<iostream>
#include<map>
using namespace std;
struct dian{
	int x,y;
	/*bool operator<(const dian &a) const
	{
		return a.x>x;
	}*/
	/*自定义类型,使用带排序效果的容器需要提前自己写好排序函数*/ 
};
bool operator<(const dian &a,const dian &b)
{
	return a.x<b.x;
} 
/* 这个排序函数既可以写在里面,也可以写在外面*/
void swap(map<dian,int> &m,map<dian,int> &p)
{
	map<dian,int> mp;
	mp=m;
	m=p;
	p=mp;
}
int main()
{
	map<dian,int> m,p;
	dian a;
	a.x=5;
	a.y=6;
	m[a]=6;
	dian b;
	b.x=7;
	b.y=8;
	m[b]=9;
	dian c;
	c.x=6;
	c.y=9;
	m.insert(pair<dian,int>(c,10));
	/* map可以直接用pair构造一个新的键值对*/ 
//	m.swap(p);
//	swap(m,p);
//	p=m;
	map<dian,int> mp(m); 
	/* map仍然可以直接=赋值,或者直接swap以及自己写swap函数*/
	for(auto i:mp)
	{
		cout<<i.first.x<<" "<<i.first.y<<" "<<i.second<<endl;
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值