I am using a set to hold structs which contain several strings. I want to be able to use the find() functionality of sets. However, since the set is holding structs, it doesn't work. I want find() to look at one of the strings in the struct when it does find. How can this be done?
Here's the code that I tried to use. It works fine except for the part where find() is used.
#include <iostream>
#include <string>
#include <set>
using namespace std;
struct test
{
string key;
string data;
};
bool operator<(const test & l, const test & r)
{
return l.key < r.key;
}
bool operator==(const test & l, const test & r)
{
return l.key == r.key;
}
set<test> s;
int main()
{
test newmember;
newmember.key = "key";
newmember.data = "data";
s.insert(newmember);
s.find("key");
}
Here are the error messages when I try to compile it:
test.cpp:30:7: error: no matching member function for call to 'find'
s.find("key");
~~^~~~
In file included from test.cpp:3:
In file included from /usr/include/c++/4.2.1/set:65:
/usr/include/c++/4.2.1/bits/stl_set.h:429:7: note: candidate function not viable: no known conversion from 'const char [4]' to 'const key_type' (aka 'const test') for 1st argument
find(const key_type& __x)
^
/usr/include/c++/4.2.1/bits/stl_set.h:433:7: note: candidate function not viable: no known conversion from 'const char [4]' to 'const key_type' (aka 'const test') for 1st argument
find(const key_type& __x) const
^
1 error generated.
To be able to put your structs into
set
you have to specifyoperator<
for your struct. You can make theoperator<
return result from comparing corresponding string members.To be able to use
find
you can specifyoperator==
for your struct to returntrue
if corresponding string members are equal.Sample:
If you want to call
find()
withstring
parameter you can provide an implicit constructor fromstring
for yourtest
struct for example like this:But usage of implicit constructors isn't a good technique, because it can lead to some unpredictable conversions somewhere further in your code.
Refer this link : find_if using vectorlist
This link will use to find the element in the vector or template list.
I suggest you
operator<
andoperator==
to your struct instead of overloading the global operator, I find it much cleaner; example:Now on to your real problem - your are passing a string to the
find()
function, but it only accepts structs of typetest
. In order to do so, add a constructor for automatic conversion, so the final struct would look like this:Then passing a string to
find()
would automatically call the constructor and create a temporarytest
struct containing only the relevant key. Note that in this special case, the constructor must not be declaredexplicit
.