Cost of using std::map with std::string keys vs int keys?

前端 未结 6 1649
礼貌的吻别
礼貌的吻别 2020-12-06 01:43

I know that the individual map queries take a maximum of log(N) time. However I was wondering, I have seen a lot of examples that use strings as map keys. What is the perfor

6条回答
  •  清歌不尽
    2020-12-06 02:11

    The cost difference will be linked to the difference in cost between comparing two ints versus comparing two strings.

    When comparing two strings, you have to dereference a pointer to get to the first chars, and compare them. If they are identical, you have to compare the second chars, and so on. If your strings have a long common prefix, this can slow down the process a bit. It is very unlikely to be as fast as comparing ints, though.

提交回复
热议问题