STL MAP should use find() or [n] identifier to find element in map?

前端 未结 4 1621
离开以前
离开以前 2020-12-05 20:37

I am confused which is more efficient?

As we can access map directly, why do we need to use find?

I just need to know which way is more efficient.



        
4条回答
  •  悲&欢浪女
    2020-12-05 21:04

    The [] operator in map is not constant it is logarithmic. Most of the books stress on this fact and point out it is a bit misleading. So both find and [] operator are with the same complexity.

    Please note that the [] operator will create the entry even if it does not exist while find will return end() in that case.

提交回复
热议问题