简体   繁体   中英

Why doesn't count return the correct value for a set C++ object?

I have a set object containing struct data elements. However, count seems to be returning a "1" when the object isn't contained within the set object. It seems like it is only looking at the first element of the struct. What am I doing wrong? Here is an example of my code:

I did implement the "<" and "==" operators as 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 is where I check to see if the objects exist and add them if the 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 the detail if this detail has not been saved
       savedRxDetails.insert(drugKey);      
    }
    else {
        return;
    }

I added the following four values to "savedRxDetails":

alert id   = E51ED799-10C5-475F-9474-1A403B85A83C
patient_id = 4513004328217
claim_id   = 126872102351
ndc_code   = 55111059430

Then the next call to this code, the following struct values are checked to see if they exist within savedRxDetails. When I call "savedRxDetails.count(drugKey)" with the values below, the value returned is "1":

 alert id   = E51ED799-10C5-475F-9474-1A403B85A83C
 patient_id = 4513004328217
 claim_id   = 114225128231
 ndc_code   = 00006027531

You can see the first element (alert_id) and second element (patient_id) of the struct matches, but none of the rest of it does. Do I need to implement some other operator other than "<" and "==" to get the "count" method to work correctly?

You didn't implement the comparison correctly. You're supposed to compare only the first fields that 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 is much more easily 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);

This will return false if lhs.alert_uuid == rhs.alert_uuid , which I suspect is wrong.

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);
 };

I suspect what you want is:

inline bool operator<(const drugKey_tp &lhs,
                       const drugKey_tp &rhs) {
     if (lhs.alert_uuid != rhs.alert_uuid)
     {
         return lhs.alert_uuid < rhs.aler_uuid;
     }
     if (lhs.ndc_code != rhs.ndc_code)
     {
         return lhs.ndc_code < rhs.ndc_code;
     }
     ... and so on ... 
 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM