What are the advantages/disadvantages in using std::map's interator/[] operator

This is a discussion on What are the advantages/disadvantages in using std::map's interator/[] operator within the C++ Programming forums, part of the General Programming Boards category; What are the advantages/disadvantages in using std::map's interator or using [] operator? For example: Code: map<char,int> ht; cout<<ht['s']<<endl; ht['s']++; map<char,int>::iterator ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What are the advantages/disadvantages in using std::map's interator/[] operator

    What are the advantages/disadvantages in using std::map's interator or using [] operator?

    For example:
    Code:
        map<char,int> ht;                                                                                                                                                        
        cout<<ht['s']<<endl;                                                                                                                                                     
        ht['s']++;                                                                                                                                                               
        map<char,int>::iterator it = ht.find('s');                                                                                                                               
        cout<<it->second<<endl;

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    The [] operator adds a blank entry to the map if it does not exist. If the find() function does not find the entry it simply points to the end of the map. (the map is not modified in any way)

    Code:
    map<int, int> foo;
    foo[5] = 5; // Add an entry with key 5 and value 5.
    
    int a = foo[5]; // Retrieve value at key 5 (which is 5)
    map<int, int>::iterator f = foo.find(5); // Returns a valid iterator as key 5 exists.
    map<int, int>::iterator g = foo.find(6); // Returns an "invalid" iterator as key 6 does not exist.  No modifications to the map are made.
    cout << foo[6]; // Output value at key #6.. wait key #6 does not exist.. Adds key #6 to the map and sets to zero and outputs zero.  So now the map has grown.
    Not sure if that answered your question or not. I don't know if cout << foo[6]; is undefined behavior or if that is valid code. i.e.

    Code:
    struct foo{
    int        b;
    DWORD d;
    };
    
    map<int. foo> mapOne;
    mapOne[5]; // Add a new entry with key 5.  Does the value struct foo get initialized to zero? I didn't bother checking.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> Does the value struct foo get initialized to zero?
    Yes. If you use operator[] and the key does not exist then a value is created and default initialized.

    I think that is the biggest difference between the iterator and the operator[].

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    And find has a const version which allow to express (and assure) that your algorithm will not change the map.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21