I have a set storing self defined data, and I'd like to search it by key value.
Here is a simple example:
struct Data {
Data(int x, int y):x(x), y(y){}
int x;
int y;
};
struct Compare {
bool operator() (const Data& lhs, const Data& rhs) {
return lhs.x < rhs.x;
}
};
int main()
{
set<Data, Compare> s;
s.insert(Data(2, 77));
s.insert(Data(3, 15));
s.insert(Data(1, 36));
for (auto i:s)
cout<<i.x<<" "<<i.y<<endl;
auto i = s.find(3); // <-- Not working
auto i = s.find(Node(3, 0));
if (i != s.end())
cout<<"found"<<endl;
else
cout<<"not found"<<endl;
}
This set is sorted only by x
, but I have to create a temp object to search in the set: s.find(Node(3, 0))
Is it possible to search by key only? i.e. s.find(3)
?