Question

I have defined a map,

static std::map<std::string, std::string> myentries;

entries are filled dynamically through key and value strings, the number of elements and sequence is dynamic ..

myentries[key] = value; 

now I have to retrieve 7 parameters from the map whose key's are know to me example, hostName, port, user1, user2, user3

one way is to access all 5 parameters and have 5 statements like ..

std::map<std::string, std::string>::iterator iterparam1 = entries_.find("hostName");

and access iterparam1->second.data(), iterparam2->second.data() ..

This simple approach does its job as I know the subset from the map, however I would like to discuss if there are any alternative's to this way with benefits and drawbacks of current verses the suggested/alternate approach ..

Thanks for your time :)

Was it helpful?

Solution

I believe the wrong thing with your approach is the way you decided to save a host's parameters. Better store a structure of type Host(custom defined structure) than a set of key value pairs. Otherwise at some point in your code you will have to somehow map key names to actual fields in the structure which will make the code hard to maintain and less safer. Don't try to work around classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top