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.