I have a std :: map map<string , Property*> _propertyMap
, where string
is the name of the property and Property*
contains the values of the properties.
I need to process the values of the properties and convert them to a specific data format - each property has its own format, for example, if the initialization of the map is as follows:
_propertyMap["id"] = new Property(Property::UUID, "12345678"); _propertyMap["name"] = new Property(Property::STRING, "name"); ....
then "id"
should be handled differently than "name"
, etc.
This means that I need to search for each property on the map and process its values accordingly.
I thought of two ways of doing this.
One, use the std::map::find
method to get a specific property, for example:
map<string , Property*>::iterator it1 = _propertyMap.find("id"); if(it1 != _propertyMap.end()) { //element found - process id values } map<string , Property*>::iterator it2 = _propertyMap.find("name"); if(it2 != _propertyMap.end()) { //element found - process name values } ....
Two, iterate over the map and for each record check what the name of the property is and do it accordingly:
for (it = _propertyMap.begin(); it != _propertyMap.end(); ++it ) { //if it is events - append the values to the matching nodes if (it->first == "id") { //process id values } else if (it->first == "name") { //process name values } ..... }
Given that the time complexity std :: map :: find is O (logN) , the complexity of the first solution is O(NlogN)
. I'm not sure about the complexity of the second solution, because it iterates over the map once ( O(N)
), but it does a lot of if-else
each iteration. I tried to spread general map::find()
google questions, but could not find any useful information; most of them just have to get one value from the map, and then find()
does it with better complexity ( O(logN) vs O(N)
).
What is the best approach? or maybe there is another one that I have not thought about?
In addition, the styling code says, which one is better and more understandable code?