Boost logo

Boost :

Subject: [boost] std::map<> - like structure for other than std::pair<>?
From: David Hagood (david.hagood_at_[hidden])
Date: 2015-04-14 07:26:51


Several times I have run into a use case where I have some object that I
would like to access via something like a std::map<>, but which already
contains a member that I would like to use as a key. Is there any data
structure that "acts like a map", but instead of storing
std::pair<key,value> allows me to store an object directly, and specify
what member of that object is the key value?

I can give a specific use case for this:

For those of you not familiar with the SCA, there is an object called
CF::Properties. This is a CORBA sequence (think std::vector) of objects
of {string name, CORBA::Any value}, used to store metadata. Ideally, I'd
like to have an object that I could initialize from that sequence that
would allow me to do things like this:

inline string &getKey(CF::Property &x)
{
    return x.name;
}

void foo(CF::Properties &meta)
{
    mapLikeThing<CF::Property,getKey> myMap(meta);
    double sampleRate;
    myMap["SampleRate"] >> sampleRate;
    setHardwareSampleRate(sampleRate);
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk