CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Prefferred method of inserting in map

    Suppose I have map
    Code:
    map<string, int> Employees;
    Which of the following method are best to use for insertion?

    1. Assignment using array index notation

    Code:
    Employees["Mike C."] = 5234;
     Employees["Charlie M."] = 3374;
    2. Assignment using member function insert() and STL pair
    Code:
    Employees.insert(std::pair<string,int>("David D.",1923));
    3. Assignment using member function insert() and "value_type()"
    Code:
    Employees.insert(map<string,int>::value_type("John A.",7582));
    4. Assignment using member function insert() and "make_pair()"
    Code:
    Employees.insert(std::make_pair("Peter Q.",5328));
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Prefferred method of inserting in map

    #1 does something different than the others. It will replace any existing entries for that key; the rest will not. It's also slightly less efficient if the key didn't previously exist since it has to default-construct a value and then overwrite it.

    For most cases in which #1 is not appropriate, I prefer #4. The exception is when make_pair() is not able to automatically determine the appropriate type. The above may actually be such a case; there's no way make_pair() would be able to tell that it should make the first type a string rather than a char*. In such a situation, I would go with #2.

  3. #3
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: Prefferred method of inserting in map

    Quote Originally Posted by Lindley View Post
    #1 does something different than the others. It will replace any existing entries for that key; the rest will not.
    std::map associate key with only one value. So, why would others not replace?

    Quote Originally Posted by Lindley View Post
    It's also slightly less efficient if the key didn't previously exist since it has to default-construct a value and then overwrite it.
    I am not sure but I think others will also be first constructed then they will be put in map. So, why inefficient?

    Quote Originally Posted by Lindley View Post
    For most cases in which #1 is not appropriate, I prefer #4. The exception is when make_pair() is not able to automatically determine the appropriate type. The above may actually be such a case; there's no way make_pair() would be able to tell that it should make the first type a string rather than a char*. In such a situation, I would go with #2.
    Sounds perfect.
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Prefferred method of inserting in map

    numbers 2,3 and 4 are exactly the same thing.

    value_type is just a typedef to pair<string, int>

    make_pair is just a way to create a pair, with implicit type.

    As lindley said 1 and 2 don't do the same thing, so it's up to you.

    ...

    ...

    The most efficient method is insert when the key is not there, and update when it is: http://stackoverflow.com/questions/9...or-stdmap-find

    However, it is a bit convoluted.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Prefferred method of inserting in map

    Quote Originally Posted by sunnypalsingh View Post
    std::map associate key with only one value. So, why would others not replace?
    'insert()' will actually fail if the given key already exists within the map.

    Quote Originally Posted by sunnypalsingh View Post
    I am not sure but I think others will also be first constructed then they will be put in map. So, why inefficient?
    The inefficiency happens whenever the key is not yet in the map. If it is in the map, '[]' simply returns a reference to the associated value and assigns the new value. However, in the case it is not, it cannot return a reference. Thus, it will create an object using the default constructor and return a reference to the object accordingly. The value then gets assigned.

    'insert()' saves the first step....and constructs the object with the given value in one step thus saving the cost of creating a temporary object.
    Last edited by Andreas Masur; April 13th, 2010 at 04:43 PM.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Prefferred method of inserting in map

    Quote Originally Posted by Andreas Masur View Post
    However, in the case it is not, it cannot return a reference. Thus, it will create a temporary object using the default constructor and return a reference to this temporary object. The then value gets assigned.
    I agree except the part where a temporary object is created. The object is nothing temporary.

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Prefferred method of inserting in map

    Quote Originally Posted by monarch_dodra View Post
    I agree except the part where a temporary object is created. The object is nothing temporary.
    Yep...that is correct...thanks for correcting me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured