#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;
}
}
map的赋值,交换以及初始化
最新推荐文章于 2023-04-03 14:02:38 发布