Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memoizing a function with two inputs in C++

Tags:

c++

I have a function, f(a,b), that accepts two inputs. I do not know ahead of time which values of a and b will be used. I'm okay with being a little wasteful on memory (I care about speed). I want to be able to check if the output of f(a,b) has already been delivered, and if so, deliver that output again without re-running through the f(a,b) process.

Trivially easy to do in Python with decorators, but C++ is way over my head here.

like image 668
John Smith Avatar asked May 31 '26 11:05

John Smith


1 Answers

I would use a std::map (or maybe an std::unordered_map) whose key is a std::pair, or perhaps use a map of maps.

C++11 improvements are probably helpful in that case. Or maybe some Boost thing.

like image 84
Basile Starynkevitch Avatar answered Jun 01 '26 23:06

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!