Блог пользователя i_anku

Автор i_anku, история, 23 месяца назад, По-английски

map is faster in cpp than unordered_map?

So today I was up solving Codeforces Round #790 (Div. 4) i got stuck in problem F bc i was using unordered_map and repetedly getting tle at 19th and 20th testcase i gave up and tried to look into solution i found out they were using map i tried it and it gets accepted instantaneously, then i wonder which map is faster, tried to look into some online resources but found oppsosite of it every online resource was say unordered_map is faster than map here is my submissions with map unordered_map someone plz help me in this i am really confused, i might get downvote for this but i just want to know the reason thank u

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
23 месяца назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится
»
23 месяца назад, # |
Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

average case time complexity : unordered_map O(1) < map O(logn)

worst case time complexity : unordered_map O(n) > map O(logn)

unordered_map suffers with collision issue if test cases are made that way. please read their internal implementation for more details.

»
23 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

unordered_map is usually faster in general and takes O(1) time for most its commands, though, on some special calls it takes O(n) time, and it's possible to generate test cases so that unordered_map takes O(n) time everytime you use it, which testers mostly put in their tests. Check out this blog for deeper understanding and on how to bypass those testcases. https://codeforces.com/blog/entry/62393