首先是find函数的应用
map<int, int>room
if(room.find(5) == room.end()) //不存在
room[5] = 1;
else
room[5] = room[5] + 1;
对于迭代器应用:
for (map<int, int>::iterator it = room.begin(); it != room.end(); it++) {
if (it->first == it->second)
return it->second;
}
对于判断某一键值存在
if (room.find(a) == room.end()) {
room[a] = 1; // 不存在
}
构造自定义类型的map
注意的时自定义的结构体要定义其排序方式
struct P {
int low, sum;
P(int minn, int summ) :low(minn), sum(summ) {};
bool operator <(const P& s)const
{
if (low == s.low)
return sum > s.sum; //最小堆
else
return low < s.low; //最大堆
}
};
map<P, int>room;