C++ Data StructuresLesson 54Free preview
C++ Maps
This lesson is part of C++ · All levels
Summary
Std::map stores key-value pairs sorted by key, so that each key is unique and can be searched quickly. The value is taken with the operator [] or at(), and a new key is automatically created when [] is used on an existing key. Maps are perfect for data named like dictionaries, ID-to-data indexes, or word count statistics.
Key points
- The map std::pair<key, value> pairs and is sorted by key.
- M[key] takes or creates entries; m.at(key) only takes and throws out_of_range when there is none.
- M.find(k) returns the iterator, and m.end() marks the key is not found.
- The iteration results in p.first (key) and p.second (value).
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.