This question references the following asked question: Using a STL map of function pointers
In C++11, I am using a map to store a <string, function>
pair to more efficiently execute code instead of using if...else if...else if
as mentioned by the referenced link.
The code is encapsulated in a member function, where this
refers to the class, allowing access to member variables.
using f = std::function<void()>;
static const std::map<std::string, f> doSumthin{
{"case 1", [this]() {
// execute code in case 1
}},
{"case 2", [this]() {
// execute code in case 2
}},
…
};
auto it = doSumthin.find(someString);
if (it != doSumthin.end())
{
it->second();
}
However, I need to make the code base work in VS 2008. I'm not sure what the most optimal way to achieve replicate the above code to work in VS 2008 without reverting back to the less efficient if...else if
.
Can I get some guidance in regards to this issue?