map如何存储自己使用的对象进行存储~~
只需要重载比较函数就可以了。例子如下
#include<stdio.h>
#include<map>
#include<string>
#include <iostream>
using namespace std;
struct node{
int x;
int y;
};
bool operator < (const node & p1 ,const node & p2) //重载比较函数 重要
{
if(p1.y == p2.y)
return p1.x < p2.x;
return p1.y < p2.y;
}
int main()
{int n;
map<node,int> mymap;//定义自己的map
string s,str;
int i;
for(i = 0 ; i < 5 ; i++)
{
node p;
p.x = p.y =0;
cin>>p.x>>p.y;
mymap[p]++; //直接使用[]就可以进行定位了
//cout<<mymap[p]<<endl;
}
map<node,int>::iterator ite;
for(ite = mymap.begin();ite != mymap.end(); ite++)
cout<< "x = "<<ite->first.x << " y = "<< ite->first.y<<" "<<ite->second <<endl;
return 0;
}
而重载hash_map需要重载等于跟比较函数