<kbd id="5sdj3"></kbd>
<th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>

    #tsl::robin_map# 關(guān)聯(lián)式容器

    共 7323字,需瀏覽 15分鐘

     ·

    2023-08-07 22:02

     文章所涉及內(nèi)容更多來自網(wǎng)絡(luò),在此聲明,并感謝知識的貢獻(xiàn)者!

    關(guān)聯(lián)式容器類型
    關(guān)聯(lián)式容器類型
    map: 紅黑樹 具有自動排序的功能,是非嚴(yán)格的二叉搜索樹。map內(nèi)部的所有元素都是有序的,使用中序遍歷可將鍵值按照從小到大遍歷出來。
    優(yōu)點(diǎn):有序性,內(nèi)部實現(xiàn)的紅黑樹的查找,插入和刪除的復(fù)雜度都是O(logn),因此效率非常高。
    缺點(diǎn):空間占用率高,因為map內(nèi)部實現(xiàn)了紅黑樹,雖然提高了運(yùn)行效率,但是因為每一個節(jié)點(diǎn)都需要額外保存父節(jié)點(diǎn),孩子節(jié)點(diǎn)和紅。黑性質(zhì),使得每一個節(jié)點(diǎn)都占用大量的空間。
    適用:對于有順序要求的問題,用map會高效一些。
    unordered_map: 哈希表(也叫散列表,通過關(guān)鍵碼值映射到Hash表中一個位置來訪問記錄,查找的復(fù)雜度是O(1)。無序的 (海量數(shù)據(jù)廣泛應(yīng)用)。
    優(yōu)點(diǎn):因為內(nèi)部實現(xiàn)哈希表,因此其查找速度非???br>缺點(diǎn):哈希表的建立比較耗費(fèi)時間,有可能還會哈希沖突(開鏈法避免地址沖突)
    適用:常用于查找問題。
    tsl
    使用循環(huán)哈希的快速哈希映射和哈希集的C++實現(xiàn)
    循環(huán)映射庫是一個快速哈希映射和哈希集的C++實現(xiàn),使用開放尋址和線性循環(huán)哈希以及后移刪除來解決沖突。
    提供了四個類:tsl::robin_map、tsl::robin_set、tsl::robin_pg_map和tsl::robin_pg _set。前兩種速度更快,使用二次冪增長策略,后兩種使用主要增長策略,能夠更好地應(yīng)對糟糕的哈希函數(shù)。
    據(jù)傳:用 tsl::robin_map 替換了原來的 boost::unordered_map,整體性能提升了 5 倍

    關(guān)聯(lián)式容器函數(shù)

    關(guān)聯(lián)式容器的常用函數(shù):
    map的所有成員函數(shù)的列表:
    構(gòu)造函數(shù)/析構(gòu)函數(shù)
    Functions     Description
    constructors     Construct map
    destructors     Map destructor
    operator=     Copy elements of the map to another map.
    迭代器
    Functions     Description
    begin     Returns an iterator pointing to the first element in the map.
    cbegin     Returns a const iterator pointing to the first element in the map.
    end     Returns an iterator pointing to the past-end.
    cend     Returns a constant iterator pointing to the past-end.
    rbegin     Returns a reverse iterator pointing to the end.
    rend     Returns a reverse iterator pointing to the beginning.
    crbegin     Returns a constant reverse iterator pointing to the end.
    crend     Returns a constant reverse iterator pointing to the beginning.
    容量
    Functions     Description
    empty     Returns true if map is empty.
    size     Returns the number of elements in the map.
    max_size     Returns the maximum size of the map.
    元素訪問
    Functions     Description
    operator[]     Retrieve the element with given key.
    at     Retrieve the element with given key.
    修飾符
    Functions     Description
    insert     Insert element in the map.
    erase     Erase elements from the map.
    swap     Exchange the content of the map.
    clear     Delete all the elements of the map.
    emplace     Construct and insert the new elements into the map.
    emplace_hint     Construct and insert new elements into the map by hint.
    觀察者
    Functions     Description
    key_comp     Return a copy of key comparison object.
    value_comp     Return a copy of value comparison object.
    運(yùn)作方式
    Functions     Description
    find     Search for an element with given key.
    count     Gets the number of elements matching with given key.
    lower_bound     Returns an iterator to lower bound.
    upper_bound     Returns an iterator to upper bound.
    equal_range     Returns the range of elements matches with given key.
    分配者
    Functions     Description
    get_allocator     Returns an allocator object that is used to construct the map.
    非成員重載函數(shù)
    Functions     Description
    operator==     Checks whether the two maps are equal or not.
    operator!=     Checks whether the two maps are equal or not.
    operator<     Checks whether the first map is less than other or not.
    operator<=     Checks whether the first map is less than or equal to other or not.
    operator>     Checks whether the first map is greater than other or not.
    operator>=     Checks whether the first map is greater than equal to other or not.
    swap()     Exchanges the element of two maps.



    關(guān)聯(lián)式容器-tsl

    Tsl Github
    https://github.com/Tessil/robin-map

    Tsl 特點(diǎn)
    ?  僅頭文件
    ?  速度快
    ?  高效序列化
    ?  API類似std::unordered_map和std::unordered_set

    tsl 案例:
     

    // Test.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。
    //
    #include <cstdint>
    #include <iostream>
    #include <string>
    #include "tsl/robin_map.h"
    #include "tsl/robin_set.h"

    int main() {
        tsl::robin_map<std::string, int> map = { {"a", 1}, {"b", 2} };
        map["c"] = 3;
        map["d"] = 4;

        map.insert({ "e", 5 });
        map.erase("b");

        std::cout << map["e"] << std::endl;
        std::cout << map["f"] << std::endl;

        for (auto it = map.begin(); it != map.end(); ++it) {
            //it->second += 2; // Not valid.
            it.value() += 2;
        }

        // {d, 6} {a, 3} {e, 7} {c, 5}
        for (const auto& key_value : map) {
            std::cout << "{" << key_value.first << ", " << key_value.second << "}" << std::endl;
        }


        if (map.find("a") != map.end()) {
            std::cout << "Found \"a\"." << std::endl;
        }

        const std::size_t precalculated_hash = std::hash<std::string>()("a");
        // If we already know the hash beforehand, we can pass it in parameter to speed-up lookups.
        if (map.find("a", precalculated_hash) != map.end()) {
            std::cout << "Found \"a\" with hash " << precalculated_hash << "." << std::endl;
        }


        /*
         * Calculating the hash and comparing two std::string may be slow.
         * We can store the hash of each std::string in the hash map to make
         * the inserts and lookups faster by setting StoreHash to true.
         */
        tsl::robin_map<std::string, int, std::hash<std::string>,
            std::equal_to<std::string>,
            std::allocator<std::pair<std::string, int>>,
            true> map2;

        map2["a"] = 1;
        map2["b"] = 2;

        // {a, 1} {b, 2}
        for (const auto& key_value : map2) {
            std::cout << "{" << key_value.first << ", " << key_value.second << "}" << std::endl;
        }

        tsl::robin_set<int> set;
        set.insert({ 1, 9, 0 });
        set.insert({ 2, -1, 9 });

        // {0} {1} {2} {9} {-1}
        for (const auto& key : set) {
            std::cout << "{" << key << "}" << std::endl;
        }
    }

    參考資料
    參考資料:
    https://zhuanlan.zhihu.com/p/266741325
    https://blog.csdn.net/yelede2009/article/details/120186206
    https://blog.csdn.net/hatlonely/article/details/81349986
    http://www.aiuxian.com/article/p-bkrgjqnd-ke.html
    https://www.codfox.com/archives/20220121-1.html
    https://www.imangodoc.com/16691.html





    瀏覽 234
    點(diǎn)贊
    評論
    收藏
    分享

    手機(jī)掃一掃分享

    分享
    舉報
    評論
    圖片
    表情
    推薦
    點(diǎn)贊
    評論
    收藏
    分享

    手機(jī)掃一掃分享

    分享
    舉報

    <kbd id="5sdj3"></kbd>
    <th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>
    老熟妇久久久XXX预见频 | 尤物最新网址 | 手机在线看A片 | 免费在线亚洲 | 色婷婷视频一区二区 |