Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a filled map, which I need to rearrange according key. Such rearranging requires a lot of 'if' condition, which i found quite ugly.

please advise me any better suggestion.

thanks

What I have tried:

for (auto it = inventory_data.begin(); it != inventory_data.end(); it++){
      if(it->first == "device_model"){
        hardware_info.set ("model", it->second);
      }
      if (it->first == "device_serial"){
        hardware_info.set ("serialNumber", it->second);
      }
      if (it->first == "device_revision"){
        hardware_info.set ("revision", it->second);
      }
      if (it->first == "imei_number"){
        mobile_info.set ("imei", it->second);
      }
      if (it->first == "mobile_cellId"){
        mobile_info.set ("cellId", it->second);
      }
      if (it->first == "mobile_iccid"){
        mobile_info.set ("iccid", it->second);
      }
      if (it->first == "msisdn"){
        mobile_info.set ("msisdn", it->second);
      }
      if (it->first == "imsi"){
        mobile_info.set ("imsi", it->second);
      }
      if (it->first == "mcc"){
        mobile_info.set ("mcc", it->second);
      }
      if (it->first == "mnc"){
        mobile_info.set ("mnc", it->second);
      }
      if (it->first == "lac"){
        mobile_info.set ("lac", it->second);
      }
    }
Posted
Updated 3-Jul-17 21:35pm
Comments
Mohibur Rashid 4-Jul-17 1:44am    
for (auto it = inventory_data.begin(); it != inventory_data.end(); it++){
if(it->first == "device_model"){
hardware_info.set ("model", it->second);
} else if (it->first == "device_serial"){
hardware_info.set ("serialNumber", it->second);
} else if (it->first == "device_revision"){
hardware_info.set ("revision", it->second);
} else if (it->first == "imei_number"){
mobile_info.set ("imei", it->second);
} else if (it->first == "mobile_cellId"){
mobile_info.set ("cellId", it->second);
} else if (it->first == "mobile_iccid"){
mobile_info.set ("iccid", it->second);
} else {
mobile_info.set (it->first, it->second);
}
}

how about this?

use switch instead of if . it is better solution when you have multiple condition and you know result as well
 
Share this answer
 
Comments
Klaus-Werner Konrad 5-Jul-17 6:51am    
This will not work, because in C you can use Switch only with integral types, and a string in C is an ARRAY of characters ...
An alternative could be using a map, having the string as key value and a struct (containing the method to call and the method argument) as mapped value.
 
Share this answer
 
How about using a dictionary to resolve this? Your whole loop is then reduced to 1 line, and future extensions become very easy (and error-proof).
 
Share this answer
 
Comments
CPallini 4-Jul-17 3:46am    
We call it 'map'. :-)
Gaston Verelst 4-Jul-17 5:52am    
Sounds good to me :-)
thanks all.

I'd like to avoid such if / if-else statements @ Mohibur Rashid

My condition checking is string, not int @ Raunak Gupta

I've got a std::map which is already filled, and i need to iterate through the key to compare @CPallini
 
Share this answer
 
Comments
CPallini 4-Jul-17 2:56am    
I suggested to use another map to link the string with a {method, method_argument} struct.
RaunakGupta 5-Jul-17 1:24am    
you can use string in switch case also..
check this https://www.dotnetperls.com/string-switch

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900