std::unordered_set::find

Hi, I have a question about searching in a std::unordered_set:

I'm storing custom classes in my set, the classes have a data member (std::string in my case). I want to search the set by giving a string, not an instance of the class. Like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A
{
public:
A( std::string data ):data(data){}
bool operator==( A& rhs ) { return data == rhs.data; } //for set
std::string data;
}

int main()
{
std::unordered_set<A> s;
s.emplace("abc");
s.emplace("def");
//s.find( std::string("abc")); //not working, s.find wants argument of type T, in this case A
}


This example is just short, my class needs more than the string for the constructor, so no implicit conversion from string to A.

My idea was to use map instead of set and use the string additionaly as key, but this seems not so nice, because i have the string twice (once as key in the map and as data in my class).
Is there a better way to do that?

Christian
Topic archived. No new replies allowed.