Lesson 13 of 24

map மற்றும் unordered_mapmap and unordered_map

10 min 35 XP

map ஒவ்வொரு key-க்கும் ஒரு value, keys வரிசைப்படுத்தப்பட்டிருக்கும் (O(log n)). unordered_map வரிசை இல்லை, ஆனால் சராசரியாக O(1). m[key]++ மூலம் எண்ணுவது மிக எளிது.

A map stores a value per key with keys kept sorted (O(log n)). unordered_map has no order but averages O(1). Counting is as simple as 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() பயன்படுத்துங்கள்.

for (auto& [key, value] : m) uses C++17 structured bindings. Reading m[key] inserts the key with 0 if it's missing; use count() or find() just to check.

Challenge

+35 XP

n, பிறகு n சொற்களைப் படித்து, ஒவ்வொரு சொல்லும் எத்தனை முறை வந்தது என்று அகர வரிசையில் print செய்யுங்கள். Input: 6 apple mango apple kiwi mango apple Output: apple 3 kiwi 1 mango 2

Read n and then n words. Print how many times each word appears, in alphabetical order. 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.