Why doesn't count return the correct value for a set C++ object? -
i have set object containing struct data elements. however, count seems returning "1" when object isn't contained within set object. seems looking @ first element of struct. doing wrong? here example of code:
i did implement "<" , "==" operators well:
struct drugkey_tp { std::string alert_uuid; std::string patient_id; std::string claim_id; std::string ndc_code; }; inline bool operator<(const drugkey_tp &lhs, const drugkey_tp &rhs) { return (lhs.alert_uuid < rhs.alert_uuid && lhs.ndc_code < rhs.ndc_code && lhs.claim_id < rhs.claim_id && lhs.ndc_code < rhs.ndc_code); }; inline bool operator==(const drugkey_tp &lhs, const drugkey_tp &rhs) { return (lhs.alert_uuid == rhs.alert_uuid && lhs.patient_id == rhs.patient_id && lhs.claim_id == rhs.claim_id && lhs.ndc_code == rhs.ndc_code); };
here check see if objects exist , add them if don't:
drugkey_tp drugkey; static set<drugkey_tp> savedrxdetails; ... drugkey.alert_uuid = <some value>; drugkey.patient_id = <some value>; drugkey.claim_id = <some value>; drugkey.ndc_code = <some value>; if (savedrxdetails.count(drugkey) == 0) { // save detail if detail has not been saved savedrxdetails.insert(drugkey); } else { return; }
i added following 4 values "savedrxdetails":
alert id = e51ed799-10c5-475f-9474-1a403b85a83c patient_id = 4513004328217 claim_id = 126872102351 ndc_code = 55111059430
then next call code, following struct values checked see if exist within savedrxdetails. when call "savedrxdetails.count(drugkey)" values below, value returned "1":
alert id = e51ed799-10c5-475f-9474-1a403b85a83c patient_id = 4513004328217 claim_id = 114225128231 ndc_code = 00006027531
you can see first element (alert_id) , second element (patient_id) of struct matches, none of rest of does. need implement other operator other "<" , "==" "count" method work correctly?
you didn't implement comparison correctly. you're supposed compare first fields differ:
if (lhs.alert_uuid != rhs.alert_uuid) return lhs.alert_uuid < rhs.alert_uuid; else if (lhs.ndc_code != rhs.ndc_code) return lhs.ndc_code < rhs.ndc_code; else if ( /* etc ... */ )
this more done using std::tuple
's comparison:
#include <tuple> return std::tie(lhs.alert_uuid, lhs.ndc_code, lhs.claim_id, lhs.patient_id) < std::tie(rhs.alert_uuid, rhs.ndc_code, rhs.claim_id, rhs.patient_id);
Comments
Post a Comment