【发布时间】:2025-11-27 17:55:02
【问题描述】:
我想在我的项目中做一些缓存。
让我的API是int foo(int a, float b, float c, int d, char e)
现在在我的项目中,有很多对上述耗时 API 的调用,其中重复值 a、b、c、d 和 e。现在我想用这些参数作为键来存储这个函数的返回值。
假设我的调用顺序是
foo(23, 3.45, 4.5, 90, 'd') // returns 1000, so I need to store it in cache as (23,3.45, 4.5, 90, 'd')->1000
foo(30, 1.2, 3.5, 100, 'e') // returns 2000, so I need to store it in cache as (30, 1.2, 3.5, 100, 'e')->2000
foo(23, 3.45, 4.5, 90, 'd') // No need to call this API, I just check in my cache value associated with
//(23, 3.45, 4.5, 90, 'd'), which is already stored as 1000
在 C++ 中实现上述最佳策略应该是什么?哪种数据结构最适合做缓存表?
【问题讨论】:
-
你在 [c] 和 [c++] 上都标记了这个问题;您想要哪种语言的答案?
-
没有 C/C++ 这样的东西。 C++ 解决方案或 C 解决方案将大不相同,并且可能彼此不兼容。如果您的目标是易用性,我建议使用 C++。
标签: c++ c caching data-structures stl