用stl map时直接insert,提高效率,不需要先find,再insert

 

map::insert() will only create:

using std::cout; using std::endl; 
typedef std::map<int, std::string> MyMap; 
MyMap map; 
// ... 
std::pair<MyMap::iterator, bool> res = map.insert(std::make_pair(key,value)); 
if ( ! res.second ) { 
    cout << "key " <<  key << " already exists " 
         << " with value " << (res.first)->second << endl; 
} else { 
    cout << "created key " << key << " with value " << value << endl; 
} 

For most of my apps, I usually don't care if I'm creating or replacing, so I use the easier to read map[key] = value.

你可能感兴趣的:(项目经验)