c++STL

C++11常用特性的使用经验总结

std::unordered_map与std::map用法基本差不多,std::map使用的数据结构为二叉树,而std::unordered_map内部是哈希表的实现方式;

    // std::unordered_map与std::map用法基本差不多,但STL在内部实现上有很大不同,
    // std::map使用的数据结构为二叉树,而std::unordered_map内部是哈希表的实现方式,
    // 哈希map理论上查找效率为O(1)。但在存储效率上,哈希map需要增加哈希表的内存开销。
    std::unordered_map<std::string, std::string> mymap =
    {
        { "house","maison" },
        { "door","porte" },
        { "grapefruit","pamplemousse" }
    };
    // 1.统计长度
    // int n = mymap.bucket_count();
    int n = mymap.size();
    cout << "unordere_map length: " << n << endl;
    // 2.新建 k,v 对
    mymap["ooo"] = "aaa";
    // 3.遍历
    for(auto it = mymap.begin(); it != mymap.end(); it++){
        cout << it->first << ": " << it->second << endl;
    }
    // 4.移除 k,v 对
    // // 4.1 指定目标键值对位于 map 容器中的位置, // 其中,position 为迭代器,指向要删除的键值对。同时该方法会返回一个 iterator 迭代器,其指向的是删除键值对之后的那个键值对。
    // iterator erase (const_iterator position);

    // // 4.2 指定目标键值对的键
    // //删除 map 容器中键为 k 的键值对。由于 map 容器中每个键值对的键都是独一无二的,因此使用该语法格式的 erase() 方法,其返回值最大为 1。
    // size_type erase (const key_type& k);

    // // 4.3 删除 map 容器中位于 [first,last) 区域内的所有键值对
    // iterator erase (const_iterator first, const_iterator last);
    mymap.erase("ooo");

    // 5. 查找
    auto it = mymap.find("house");
    cout << it->first << " " << it->second << endl;      // house maison

 

 

https://iowiki.com/cpp_standard_library/cpp_unordered_set_find.html 

 

unordered_set 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

posted @ 2022-11-18 01:37  7aughing  阅读(15)  评论(0编辑  收藏  举报