std::map insert or std::map find?

后端 未结 8 1371
青春惊慌失措
青春惊慌失措 2020-12-12 08:41

Assuming a map where you want to preserve existing entries. 20% of the time, the entry you are inserting is new data. Is there an advantage to doing std::map::find then std

8条回答
  •  离开以前
    2020-12-12 09:18

    I would think if you do a find then insert, the extra cost would be when you don't find the key and performing the insert after. It's sort of like looking through books in alphabetical order and not finding the book, then looking through the books again to see where to insert it. It boils down to how you will be handling the keys and if they are constantly changing. Now there is some flexibility in that if you don't find it, you can log, exception, do whatever you want...

提交回复
热议问题