Index | Diary 2024-04-30

头文件

    #include <map>
    #include <unordered_map>

与 map 区分

map 中存储的数据是有序的,内部实现了一个红黑树

unordered_map 内部实现了一个 hash table, 是无序的

初始化

std::unorderd_map<std::string, std::string> umap; 可存储<string, string> 键值对的容器

std::unordered_map<std::string, std::string> umap{
    {"淘宝","https://www.taobao.com/"},
    {"京东","https://www.jd.com/"},
    {"天猫商城","https://jx.tmall.com/"} 
};

Methods

unordered_map<int, int> hashtable;

auto it = hashtable.find( ); 以 key 为参数寻找 hmap 中的元素,返回该位置上的迭代器

cout << it -> first : it -> second << endl; 分别返回 key: value