fork download
  1. #include <list>
  2. #include <unordered_map>
  3.  
  4. class LRUCache {
  5. public:
  6. const size_t capacity;
  7. std::list<int> lru;
  8. std::unordered_map<size_t, std::list<int>::iterator> cache;
  9. std::unordered_map<size_t, size_t> key_val_map;
  10.  
  11. LRUCache(const size_t capacity) : capacity(capacity) {}
  12.  
  13. int get(size_t key) {
  14. if (key_val_map.count(key) == 0) {
  15. return -1;
  16. }
  17.  
  18. make_most_recent(key);
  19. return key_val_map[key];
  20. }
  21.  
  22. void put(size_t key, size_t value) {
  23. if (key_val_map.size() == capacity && key_val_map.count(key) == 0) {
  24. pop();
  25. }
  26.  
  27. make_most_recent(key);
  28.  
  29. key_val_map[key] = value;
  30. }
  31.  
  32. private:
  33. void make_most_recent(size_t key) {
  34. if (key_val_map.count(key)) {
  35. lru.erase(cache[key]);
  36. }
  37.  
  38. lru.push_front(key);
  39. cache[key] = lru.begin();
  40. }
  41.  
  42.  
  43. void pop() {
  44. key_val_map.erase(lru.back());
  45. cache.erase(lru.back());
  46. lru.pop_back();
  47. }
  48. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty