c++ - overloading operators for struct as map key -
hi i'm having problems overloading operator of struct use key. here struct intend use map key, has 2 char arrays:
struct fconfig { char product[3]; char exchange[4]; bool operator < (const fconfig &rhs) const { return (strcmp(product, rhs.product) < 0 || strcmp(exchange, rhs.exchange <0)); } };
my comparison long 1 of product or exchange not equal rhs's, key considered unique. use , "invalid operator <" during runtime. i'm totally new @ creating keys, i'm still having trouble understanding logic when overwriting < operator. appreciate help, thanks!
your confusion how operator <
should work pretty common. want this:
bool operator < (const fconfig &rhs) const { int product_comparision = strcmp(product,rhs.product); if (product_comparision<0) return true; if (product_comparision>0) return false; return strcmp(exchange,rhs.exchange)<0; }
since product
primary key, time consider secondary key if primary key values equal.
Comments
Post a Comment