Re: set::find with custom equality operator==
On Feb 3, 1:12 am, dragoncoder <pktiw...@gmail.com> wrote:
Hi all, I have the following code.
unsigned long long id;
inline bool operator==(const ecodb2& lhs,
const ecodb2& rhs)
std::cout << "Some text" << std::endl;
return ((lhs.id == rhs.id) || !std::strcmp(lhs.code, rhs.code));
bool operator()(const ecodb2& lhs,
const ecodb2& rhs) const
bool retval = false;
if(lhs.id < rhs.id)
retval = true;
else if(lhs.id == rhs.id)
retval = std::strcmp(lhs.code, rhs.code);
std::set<ecodb2, Compare> industry_codes;
val.id = 1;
std::strncpy(val.code, "GOV", sizeof(val.code));
std::strncpy(val.code, "ABC", sizeof(val.code));
std::set<ecodb2>::iterator it = industry_codes.find(val);
if(it != industry_codes.end())
std::cout << "Found in set" << std::endl;
std::cout << "Not found in set" << std::endl;
While running this code, I expect to see Found in set (as either id or
code match meaning a perfect match) and also "Some text" being
printed, becasue set::find should call my custom operator==() function
to find a match but the output I get is "Not found in set" and also
"Some text" is not getting printed.
I don't think it should be possible for 2 variables
of type ecodb2 to be satisfy both x < y and x == y.
This can happen with your code (in the case
where x and y share an id but not a code). My guess
would be that find checks at some point for some x and y
whether x < y, and if this is the case, it assumes that x != y,
which in your example is NOT the case.
I would recommend implementing this using two maps,
one with id as key, and one with code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]