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

后端 未结 8 1381
青春惊慌失措
青春惊慌失措 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:20

    map[ key ] - let stl sort it out. That's communicating your intention most effectively.

    Yeah, fair enough.

    If you do a find and then an insert you're performing 2 x O(log N) when you get a miss as the find only lets you know if you need to insert not where the insert should go (lower_bound might help you there). Just a straight insert and then examining the result is the way that I'd go.

提交回复
热议问题