#include <vector>
#include <algorithm>
#include <cassert>
template <class T, class Key, Key (T::*keyFn) >
class TKeyedVector : public std::vector<T>
{
public:
using Base=std::vector<T>;
using const_iterator = typename std::vector<T>::const_iterator;
using Base::cbegin;
using Base::cend;
const_iterator find(const Key& key) const
{
return std::find_if(cbegin(), cend(),
[&](const T& entry) {
return entry.*keyFn==key;
});
}
};
struct KeyedDataEntry
{
std::string key;
int value;
};
using KeyedDataArray = TKeyedVector<KeyedDataEntry, std::string, &KeyedDataEntry::key>;
int main() {
KeyedDataArray data;
data.push_back(KeyedDataEntry{"one", 1});
data.push_back(KeyedDataEntry{"two", 2});
assert(data.find("one")->value ==1);
return 0;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNhc3NlcnQ+CgoKdGVtcGxhdGUgPGNsYXNzIFQsIGNsYXNzIEtleSwgS2V5IChUOjoqa2V5Rm4pID4KY2xhc3MgVEtleWVkVmVjdG9yIDogcHVibGljIHN0ZDo6dmVjdG9yPFQ+CnsKcHVibGljOgogICAgdXNpbmcgQmFzZT1zdGQ6OnZlY3RvcjxUPjsKCXVzaW5nIGNvbnN0X2l0ZXJhdG9yID0gdHlwZW5hbWUgc3RkOjp2ZWN0b3I8VD46OmNvbnN0X2l0ZXJhdG9yOwoJdXNpbmcgQmFzZTo6Y2JlZ2luOwoJdXNpbmcgQmFzZTo6Y2VuZDsKCQogICAgY29uc3RfaXRlcmF0b3IgICAgICBmaW5kKGNvbnN0IEtleSYga2V5KSBjb25zdCAKICAgIHsKICAgIAlyZXR1cm4gc3RkOjpmaW5kX2lmKGNiZWdpbigpLCBjZW5kKCksIAogICAgCQlbJl0oY29uc3QgVCYgZW50cnkpIHsKICAgIAkJCXJldHVybiBlbnRyeS4qa2V5Rm49PWtleTsgCiAgICAJCX0pOyAKICAgIAl9Cn07CgoKc3RydWN0IEtleWVkRGF0YUVudHJ5CnsKICBzdGQ6OnN0cmluZyAgICAgICB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