Link to home
Start Free TrialLog in
Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

binary_search with LessThanComparable prototype: compilation failure!

Ah hello.

Please consider the following code, which some may see as familiar due to my earlier questions:
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

class Human
{
public:
	Human(std::string strName, int nAge) : m_strName(strName), m_nAge(nAge) {}
	std::string GetName() const { return m_strName; }
	int GetAge() const { return m_nAge; }

	bool operator<(const Human& rhs) const { return m_strName < rhs.GetName(); }
	bool operator<(const std::string& rhs) const { return m_strName < rhs; }

protected:
	std::string m_strName;	
	int m_nAge;
};

int main(int argc, char * argv[])
{
	std::vector<Human> vec;
	vec.push_back(Human("Joe", 20));
	vec.push_back(Human("Mary", 21));
	vec.push_back(Human("Arnold", 44));
	vec.push_back(Human("Calterine", 21));
	vec.push_back(Human("Calterine", 45));

	sort(vec.begin(), vec.end());
	std::vector<Human>::iterator iter = vec.begin(), iter2 = vec.end();

	bool found = binary_search( vec.begin(), vec.end(), "Mary" );

	return 0;
}

Open in new window

I was previously using the version of binary_search which took a predicate as the fourth argument (see http://www.sgi.com/tech/stl/binary_search.html - the second prototype) but after reading more about LessThanComparable I thought I would investigate this.

On VS 2005, I get the compilation error

binary '<' : no operator found which takes a right-hand operand of type 'Human' (or there is no acceptable conversion)
could be 'built-in C++ operator<(const char [5], const char [5])'
while trying to match the argument list '(const char [5], Human)'

Open in new window


on the call to binary_search.

How can I use binary_search in this manner?

TIA
SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrwad99

ASKER

Hi ZOPPO, good to see you are still active on EE after all these years :)

Yes, I tried that (that is, I had the global operator and the member operator: two global operators of (const std::string& lhs, const Human& rhs) and (const Human& lhs, const std::string& rhs) is the same and removes the need for the member version) and it does indeed work.  I figured it would, since defining the global operator< is essentially the version of binary_search() which accepts a StrictWeakOrdering object, but wanted to see if there was any way I could do away with this altogether and define everything as members of the Human class...I guess there isn't, since we, when it boils down, need an operator<(Human&) within std::string to make this work, I believe...

Please correct me if this is wrong...

As a second question related to this, the documentation for StrictWeakOrdering at http://www.sgi.com/tech/stl/StrictWeakOrdering.html is very clear on what it should return:

"A Strict Weak Ordering is a Binary Predicate that compares two objects, returning true if the first precedes the second"

However, the documentation for LessThanComparable (http://www.sgi.com/tech/stl/LessThanComparable.html) does not have anything of the sort from what I can see.  So, how do I know if I am supposed to (in the example I have given) code
bool operator<(const Human& rhs) const { return m_strName < rhs.GetName(); }

Open in new window

or

bool operator<(const Human& rhs) const { return rhs.GetName() < m_strName ; }

Open in new window


Can anyone advise on this?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrwad99

ASKER

Yes Sara I was, but thanks for participating anyway.
Avatar of mrwad99

ASKER

Thanks ZOPPO!
Avatar of mrwad99

ASKER

Umm I accepted the wrong answer there; many apologies ZOPPO - I can request CS to get it changed if you want...?
No problem at all :o)

Sara's comment even is a possible solution, so it's not wrong to accept it ...

Have a nice day,

best regards,

ZOPPO
Sara's comment even is a possible solution, so it's not wrong to accept it ...
you are very kind, ZOPPO.

thanks to both of you.

Sara