题目:来自脑客爱刷题
class Value
{
public:
int money;
unsigned long long timeslot;//增加一个时间戳
Value(){}
Value(int m, unsigned long long t) :money(m), timeslot(t){}
};
class HashMap//键是string(员工姓名),值是int(员工工资)
{
public:
hash_map<string, Value> hashmap;
unsigned long long time;//插入的时间,初始化为1,每次插入记录或setAll时都会加1
unsigned long long curtime;//为setAll函数服务,记录最后一次setAll的时间
int curmoney;//记录最后一次setAll的工资
HashMap() :curmoney(-1), curtime(0),time(1){}
void put(const string &s, int i)
{
Value tmp(i, time);
hashmap[s] = tmp;
time++;//时间加1
}
int get(const string &key)
{
if (hashmap.find(key) == hashmap.end())
return -1;//找不到记录
else
{
unsigned long long time2= hashmap[key].timeslot;
if (time2 > curtime)
return hashmap[key].money;
else
return curmoney;//setAll操作的结果
}
}
bool containsKey(const string &key)
{
if (hashmap.find(key) == hashmap.end())
return false;
else
return true;
}
void setAll(int money)
{
curtime = time;
curmoney = money;
time++;
}
};