J
JC
I have a class where each instance has a unique ID associated with it,
like this (I use "..." to mean "other things", not a variable
parameter list):
typedef ... ID;
class Data {
public:
Data (ID id, ...);
bool operator < (const Data &d) const throw () { return id_ <
d.id_; }
...
private:
ID id_;
...
};
Instances are equivalent if their IDs are equivalent. I am storing
these in an std::set<Data>. I'd like to be able to erase elements from
the set given only an ID, that is:
std::set<Data> somedata = ...;
ID id = ...;
somedata.erase(id);
I've found two ways to make this work, neither are ideal for reasons I
don't want to get in to:
1) Provide an implicit Data(ID) constructor to construct dummy Data
instances used for ID comparisons.
2) Use an std::map<ID,Data> instead.
Is there a way I can erase elements from the set by ID without
constructing dummy Data instances, and without using some other
container type instead?
Thanks!
Jason
like this (I use "..." to mean "other things", not a variable
parameter list):
typedef ... ID;
class Data {
public:
Data (ID id, ...);
bool operator < (const Data &d) const throw () { return id_ <
d.id_; }
...
private:
ID id_;
...
};
Instances are equivalent if their IDs are equivalent. I am storing
these in an std::set<Data>. I'd like to be able to erase elements from
the set given only an ID, that is:
std::set<Data> somedata = ...;
ID id = ...;
somedata.erase(id);
I've found two ways to make this work, neither are ideal for reasons I
don't want to get in to:
1) Provide an implicit Data(ID) constructor to construct dummy Data
instances used for ID comparisons.
2) Use an std::map<ID,Data> instead.
Is there a way I can erase elements from the set by ID without
constructing dummy Data instances, and without using some other
container type instead?
Thanks!
Jason