-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0146 LRU缓存机制 - prac.cpp
More file actions
67 lines (59 loc) · 1.73 KB
/
0146 LRU缓存机制 - prac.cpp
File metadata and controls
67 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cstdio>
#include <unordered_map>
#include <list>
#include <utility> // pair
using namespace std;
class LRUCache {
private:
int cap;
list<pair<int,int>> dlist;
unordered_map<int, list<pair<int,int>>::iterator> map;
public:
LRUCache(int capacity) : cap(capacity) {
}
void disp() {
for (auto const x : map) {
cout << x.first << " " << (*x.second).second << ", ";
}
cout << endl;
}
int get(int key) {
if (map.count(key) == 0)
return -1;
dlist.push_front(*map[key]);
dlist.erase(map[key]);
map[key] = dlist.begin();
return dlist.front().second;
// disp();
}
void put(int key, int value) {
if (map.count(key) == 0) {
if (map.size() == cap) {
map.erase(dlist.back().first);
dlist.pop_back();
}
dlist.push_front(make_pair(key, value));
map[key] = dlist.begin();
} else {
dlist.erase(map[key]);
dlist.push_front(make_pair(key, value));
map[key] = dlist.begin();
}
// disp();
}
};
int main() {
// LRUCache lRUCache = new LRUCache(2);
LRUCache lRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {2=2, 1=1}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {3=3, 1=1}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4
return 0;
}