Creating a job search with a set of structures

I use a set to store structures containing multiple lines. I want to be able to use find () functions for collections. However, since the set contains structures, it does not work. I want find () to look at one of the lines in the structure when it finds. How can I do that?

Here is the code 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 them:

 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. 
+7
source share
3 answers

I offer you operator< and operator== for your structure, instead of overloading the global operator, I find it much cleaner; Example:

 struct test { string key; string data; bool operator<(const test& rhs) const { return key < rhs.key; } bool operator==(const test& rhs) const { return key == rhs.key; } }; 

Now about your real problem - you pass the string to the find() function, but it only accepts structures of type test . To do this, add a constructor for automatic conversion, so the final structure will look like this:

 struct test { string key; string data; test(const std::string& strKey = "", const std::string& strData = "") : key(strKey), data(strData) {} bool operator<(const test& rhs) const { return key < rhs.key; } bool operator==(const test& rhs) const { return key == rhs.key; } }; 

Then passing the string to find() will automatically call the constructor and create a temporary test structure containing only the corresponding key. Note that in this special case, the constructor should not be explicit declared.

+11
source

To place your structures in set , you must specify operator< for your structure. You can return the result of operator< to compare the corresponding members of the string.

To be able to use find , you can specify operator== so that your structure returns true if the corresponding members of the string are equal.

Example:

  // code from your question used here int main() { test newmember; newmember.key = "key"; newmember.data = "data"; test findMember; findMember.key = "key"; // as operator== and operator< doesn't care about data field we can left it be // initialized by default constructor s.insert(newmember); s.find(findMember); } 

If you want to call find() with the string parameter, you can provide an implicit constructor from string for your test structure, for example, as follows:

 struct test { //... test(const string &in_key) : key(in_key) {} //... }; 

But using implicit constructors is not a good method, because it can lead to some unpredictable conversions somewhere else in your code.

+2
source

Follow this link: find_if using vectorlist

This link will be used to search for an item in the list of vectors or patterns.

0
source

All Articles