S
shea martin
I would like to use maps as a container type, but I don't like how the
[] operator returns a default value. Is there a way to make [] return
NULL if it can't find the value?
I know I can do this:
typedef numMap map<int,myClass*>;
numMap nums;
if( nums.find(99) != nums.end() )
{
cout << "nums[99] is: " << nums[99] << endl;
}
I don't know how efficient this is, as it looks up the key 99 twice. To
avoid this situation:
numMap::iterator itr = nums.find(99);
if( itr != nums.end() )
{
cout << "nums[99] is: " << *nums << endl;
}
I don't find all this very convenient. Perhaps I am just complaining
too much.
~S
[] operator returns a default value. Is there a way to make [] return
NULL if it can't find the value?
I know I can do this:
typedef numMap map<int,myClass*>;
numMap nums;
if( nums.find(99) != nums.end() )
{
cout << "nums[99] is: " << nums[99] << endl;
}
I don't know how efficient this is, as it looks up the key 99 twice. To
avoid this situation:
numMap::iterator itr = nums.find(99);
if( itr != nums.end() )
{
cout << "nums[99] is: " << *nums << endl;
}
I don't find all this very convenient. Perhaps I am just complaining
too much.
~S