Lesson 13 of 24

map आणि unordered_map

10 min 35 XP

map प्रत्येक key साठी value साठवतो आणि keys sorted राहतात (O(log n)). unordered_map ला क्रम नसतो पण सरासरी O(1). मोजणी m[key]++ इतकी सोपी आहे.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

for (auto& [key, value] : m) C++17 चे structured bindings वापरते. m[key] वाचल्यावर key नसेल तर ती 0 सह जोडली जाते; फक्त तपासण्यासाठी count() किंवा find() वापरा.

Challenge

+35 XP

n आणि मग n शब्द वाचा. प्रत्येक शब्द किती वेळा आला ते वर्णक्रमाने print करा. Input: 6 apple mango apple kiwi mango apple Output: apple 3 kiwi 1 mango 2

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.