code_junkie

In STL maps, is it better to use map::insert than []?

Nov 12th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. map[key] = value;
  2.  
  3. using std::cout; using std::endl;
  4. typedef std::map<int, std::string> MyMap;
  5. MyMap map;
  6. // ...
  7. std::pair<MyMap::iterator, bool> res = map.insert(std::make_pair(key,value));
  8. if ( ! res.second ) {
  9. cout << "key " << key << " already exists "
  10. << " with value " << (res.first)->second << endl;
  11. } else {
  12. cout << "created key " << key << " with value " << value << endl;
  13. }
  14.  
  15. using namespace std;
  16. using namespace boost::assign; // bring 'map_list_of()' into scope
  17.  
  18. void something()
  19. {
  20. map<int,int> my_map = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
  21. }
Add Comment
Please, Sign In to add comment