Skip to content

Commit f26c24e

Browse files
committed
feat: 添加LRUCache运行示例
1 parent 343a72f commit f26c24e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
112 KB
Binary file not shown.

cache_algorithm/least_recently_used(LRU).cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
/**
4545
* 实现方法:哈希表 + 双向链表
4646
*
47-
* LRU 缓存机制可以通过哈希表辅以双向链表实现,可以用一个哈希表和一个双向链表维护所有在缓存中的简直队
47+
* LRU 缓存机制可以通过哈希表辅以双向链表实现,可以用一个哈希表和一个双向链表维护所有在缓存中的键值对
4848
* 1. 双向链表按照被使用的顺序存储这些键值对,靠近头部的键值对是最近使用的,而靠近尾部的键值对是最久未使用的。
4949
* 2. 哈希表即为普通的哈希映射(HashMap),通过缓存数据的键映射到其在双向链表中的位置。
5050
*
@@ -184,3 +184,22 @@ class LRUCache
184184
return node;
185185
}
186186
};
187+
188+
int main(int argc, const char **argv)
189+
{
190+
LRUCache *lruCache = new LRUCache(2);
191+
192+
lruCache->put(1, 1);
193+
lruCache->put(2, 2);
194+
195+
std::cout << "lruCache->get(1) = " << lruCache->get(1) << std::endl;
196+
std::cout << "lruCache->get(2) = " << lruCache->get(2) << std::endl;
197+
198+
lruCache->put(3, 3);
199+
200+
std::cout << "lruCache->get(1) = " << lruCache->get(1) << std::endl;
201+
std::cout << "lruCache->get(2) = " << lruCache->get(2) << std::endl;
202+
std::cout << "lruCache->get(3) = " << lruCache->get(3) << std::endl;
203+
204+
return 0;
205+
}

0 commit comments

Comments
 (0)