Cocos2d-x3.0模版容器具體解釋之二:cocos2d::Map<K,V>

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   strong   檔案   資料   

1.概述:

版本號碼: v3.0 beta

語言: C++


定義在 “COCOS2DX_ROOT/cocos/base” 路徑下的 "CCMap.h" 的標頭檔裡。

template <class K, class V>class CC_DLL Map;

cocos2d::Map<K,V> 是一個內部使用了 std::unordered_map的關聯容器模版。
std::unordered_map 是一個儲存了由key-value鍵值對組合成構成的關聯性容器,同意基於鍵對單個元素進行高速檢索。
在 unordered_map 中,key value 一般用來標識唯一的一個元素,而 mapped value 是一個對象,其內容關聯到對於的 key value。
在內部,unordered_map 的元素並不依賴於 key 或者 mapped 值來使用不論什麼的特定方式排序,而是取決於它們的雜湊值,以便通過他們的key值高速訪問單個元素(使用平均時間複雜度)。
在 Cocos2d-x v3.0 beta 之前,存在還有一個順序性容器 cocos2d::CCDictionary,這將會被棄用。
我們非常仔細地設計了 cocos2d::Map<K,V> 容器作為 cocos2d::CCDictionary 的替代品,所以請使用 cocos2d::Map 取代 cocos2d::CCDictionary。


2.模板參數:


K - key value的類型.
unordered_map 中的每個元素都由它的 key value 唯一標識。
V - mapped value的類型.
T 必須是一個指向 cocos2d::Object 子類對象的指標。不能是其它資料類型或者原生類型,由於我們已經將 Cocos2d-x 的記憶體管理模型整合到 cocos2d::Map<k,V> 中。


3.記憶體管理:


cocos2d::Map<K,V> 類包括了唯一一個資料成員:

typedef std::unordered_map<K, V> RefMap;RefMap _data;


_data 的記憶體管理是由編譯器自己主動處理的。假設你在棧上聲明了一個 cocos2d::Map<K,V> 對象,那就不須要關心記憶體釋放問題。


假設你調用了 new 操作符來分配一塊 cocos2d::Map<K,V> 的動態記憶體,那就須要在使用完後調用 delete 操作符來釋放記憶體。這相同適用於 new[] 和 delete[]。


注意:在新 C++ 中,它傾向於本機存放區對象而不是堆儲存物件。所以,請不要調用 new 操作符來分配 cocos2d::Map<K,V> 的堆對象,而是使用棧對象來取代。
假設你有足夠的理由在堆上動態分配 cocos2d::Map<K,V> 的話,請使用智能指標替換原始指標,比方 Shared_ptr,unique_ptr。


警告:cocos2d::Map<K,V> 不再像其它的 cocos2d 類一樣使用 retain/release和引用計數記憶體管理。


4.基本的文法:


警告: cocos2d::Map<K,V> 沒有重載 operator[],所以你不能使用像 map 這種操作來試圖從 cocos2d::Map<K,V> 擷取元素。

很多其它 API 的使用,請參考引擎原始碼和 Cocos2d-x 3.0 beta 已實現的測試例。


這裡提供一個簡單的示範範例:

//create Map<K, V> with default size and add a sprite into itauto sp0 = Sprite::create();sp0->setTag(0);Map<std::string, Sprite*> map0;std::string mapKey0 = "MAP_KEY_0";map0.insert(mapKey0, sp0);log("The size of map is %zd.",map0.size()); //create a Map<K, V> with capacity equals 5Map<std::string, Sprite*> map1(map0);std::string mapKey1 = "MAP_KEY_1";if(!map1.empty()){    auto spTemp = (Sprite*)map1.at(mapKey0);    log("sprite tag = %d", spTemp->getTag());    auto sp1 = Sprite::create();    sp1->setTag(1);    map1.insert(mapKey1, sp1);          //get all keys,stored in std::vector, that matches the object    std::vector<std::string> mapKeyVec;    mapKeyVec = map1.keys();    for(auto key : mapKeyVec)    {        auto spTag = map1.at(key)->getTag();        log("The Sprite tag = %d, MAP key = %s",spTag,key.c_str());        log("Element with key %s is located in bucket %zd",key.c_str(),map1.bucket(key));    }    log("%zd buckets in the Map container",map1.bucketCount());    log("%zd element in bucket 1",map1.bucketSize(1));      //get a random object if the map isn‘t empty, otherwise it returns nullptr    log("The random object tag = %d",map1.getRandomObject()->getTag());      //find(const K& key) can be used to search the container for an element with ‘key‘    //erase(const_iterator position) remove an element with an iterator    log("Before remove sp0, size of map is %zd.",map1.size());    map1.erase(map1.find(mapKey0));    log("After remove sp0, size of map is %zd.",map1.size());}  //create a Map<K, V> with capacity equals 5Map<std::string, Sprite*> map2(5);map2.reserve(10);  //set capacity of the map

輸出:

cocos2d: The size of map is 1.cocos2d: sprite tag = 0cocos2d: The Sprite tag = 1, MAP key = MAP_KEY_1cocos2d: Element with key MAP_KEY_1 is located in bucket 1cocos2d: The Sprite tag = 0, MAP key = MAP_KEY_0cocos2d: Element with key MAP_KEY_0 is located in bucket 0cocos2d: 2 buckets in the Map containercocos2d: 1 element in bucket 1cocos2d: The random object tag = 0cocos2d: Before remove sp0, size of map is 2.cocos2d: After remove sp0, size of map is 1.



5.最佳實務:


當將 cocos2d::Map<K, V>() 作為參數進行傳遞的時候,將它聲明為一個常引用,如 const cocos2d::Map<K, V>()&。
T 必須是是一個指向 cocos2d::Object 子類對象的指標,不能是其它資料類型或者原生類型。 


本文感謝逗比基友  偶爾e往事的大力支援:)
By:Net Fly

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.