主要看 RemoveData 函数
程序代码:
#include <iostream>
#include <map>
struct Data
{
explicit Data( int val=0 ) : value(val)
{
}
int value;
};
typedef std::map<int,Data> TDataMap;
void RemoveData( TDataMap& dm, int key )
{
TDataMap::iterator itor = dm.find(key);
if( itor != dm.end() )
{
if( itor->second.value > 1 )
--(itor->second.value);
else
dm.erase( itor );
}
}
int main()
{
TDataMap datamap;
datamap.insert( std::make_pair(0,Data(0)) );
datamap.insert( std::make_pair(1,Data(1)) );
datamap.insert( std::make_pair(2,Data(2)) );
datamap.insert( std::make_pair(3,Data(3)) );
RemoveData( datamap, 0 );
RemoveData( datamap, 1 );
RemoveData( datamap, 2 );
RemoveData( datamap, 3 );
for( TDataMap::const_iterator itor=datamap.begin(); itor!=datamap.end(); ++itor )
std::cout << itor->first << " : " << itor->second.value << '\n';
return 0;
}