wiki:hpp-map
<map>
<map> is an ordered associative container: a sorted tree of key-value pairs with O(log n) insertion, deletion, and lookup. It maintains keys in sorted order and allows iteration from min to max.
Use std::unordered_map if you don't need order; use std::map if you do or if you need to iterate a range of keys.
Example
This example builds a map of names to ages, iterates through in sorted order, and looks up a specific key.
// compile: g++ -std=c++11 -o mapexample mapexample.cpp // run: ./mapexample // description: map insertion and lookup #include <map> #include <iostream> #include <string> int main() { std::map<std::string, int> age; age["alice"] = 30; age["bob"] = 25; age["charlie"] = 35; for (const auto& [name, years] : age) { std::cout << name << ": " << years << "\n"; } std::cout << "alice is " << age["alice"] << "\n"; return 0; }
wiki/hpp-map.md · Last modified: by 127.0.0.1
