Lets say I have a class with a member variable:
std::unordered_map<KeyType, std::shared_ptr<ValueType>> myMap
and in a member function I want to do the following:
std::for_each(myMap.begin(), myMap.end(), [](std::pair<const KeyType, std::shared_ptr<ValueType>>& pair){pair.second->someMethod(); });
Is there anyway to shorten the lambda expression? I thought I could do this but it was not valid syntax:
std::for_each(myMap.begin(), myMap.end(), [](decltype(myMap::valueType)& pair){pair.second->someMethod(); });
I wouldn’t bother with a lambda at all. Unlike the other standard algorithms, for_each()
is not any more expressive or readable than the equivalent for
.
for (auto& p : myMap)
p.second->someMethod();
for (auto p = myMap.begin(); p != myMap.end(); ++p)
p->second->someMethod();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With