I have a std::map object map<string , Property*> _propertyMap
, where string
is the property's name and Property*
contains the property values.
I need to process the properties values and convert them to a specific data format- each property has its own format, e.g.. if the map initialization is as following:
_propertyMap["id"] = new Property(Property::UUID, "12345678");
_propertyMap["name"] = new Property(Property::STRING, "name");
....
then "id"
should be processed differently than "name"
etc.
This means that I need to look for each property in the map and process its values accordingly.
I thought about two ways to do that.
One, use std::map::find
method to get a specific property, like that:
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 the map and for each entry check what the property's name is and proceed 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 of 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 the map once (O(N)
), but performs a lot of if-else
each iteration. I tried to google common map::find()
questions, but couldn't find any useful information; most of them just need to get one value from the map, and then find()
does this with better complexity (O(logN) vs O(N)
).
What is a better approach? or perhaps there is another one which I didn't think of?
Also, code styling speaking, which one is more good and clear code?