1. Overview
Cocos2d::map<k,v> is an associative container template that uses Std::unordered_map internally.
Std::unordered_map is an associative container that stores a combination of Key-value key-value pairs, allowing quick retrieval of a single element based on a key.
2. Template parameters
K-key the type of value.
- The element in map is uniquely identified by its key value.
V-mapped the type of value.
- T must be a pointer to an Cocos2d::object subclass object.
3. Memory Management
If you declare a Cocos2d::map<k,v> object on the stack, you do not need to care about the memory release issue.
If you call the new operator to allocate a piece of cocos2d::map<k,v> dynamic memory, you need to call the delete operator after you have finished using it to free memory. The same applies to new[] and delete[].
Note: in new C + +, it prefers to store objects locally instead of heap storage objects. Therefore, do not call the new operator to allocate the Cocos2d::map<k,v> heap object, instead use the Stack object instead.
If you have enough reason to dynamically allocate cocos2d::map<k,v> on the heap, replace the original pointer with a smart pointer, such as SHARED_PTR,UNIQUE_PTR.
Warning:cocos2d::map<k,v> no longer uses retain/release and reference count memory management like other cocos2d classes.
4. Basic usage
Warning: cocos2d::map<k,v> does not overload operator[], so you cannot use operations like Map to try to get elements from cocos2d::map<k,v>.
For more API usage, refer to the engine source code and COCOS2D-X 3.0 Beta implemented test examples.
A simple example is provided here:
//Use the default size to create a map<k, V>, and then add a sprite to itAuto SP0 =sprite::create (); Sp0->settag (0); Map<STD::string, sprite*>MAP0;STD::stringMapKey0 ="Map_key_0"; Map0.insert (MapKey0, sp0); log ("The size of map is%zd.", Map0.size ()); //use a Map to initialize a map<k, v>MAP<STD::string, sprite*>Map1 (MAP0); std::stringMapKey1 ="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, for matching objectsSTD::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 was 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)); //if the map is not empty, get a random object from it, or return a null pointerLog"The Random Object tag =%d", Map1.getrandomobject ()Gettag ()); //Find (const k& key) can be used to search for an element in a container based on ' key '//Erase (const_iterator position) can be used to delete an element by specifying an iteratorLog"before remove sp0, size of map is%zd.", Map1.size ()); Map1.erase (Map1.find (mapKey0)); Log ("After remove Sp0, the size of map is%zd.", Map1.size ());} //creates a map<k for 5来 with the specified capacity, v>MAP<STD::string, Sprite*> MAP2 (5); Map2.reserve (Ten);//set the capacity of a map
Output results
Cocos2d:the size of Map is1. Cocos2d:sprite Tag=0cocos2d:the Sprite Tag=1, MAP key =map_key_1cocos2d:element with KEY Map_key_1 is locatedinchBuckets1cocos2d:the Sprite Tag=0, MAP key =map_key_0cocos2d:element with KEY Map_key_0 is locatedinchBuckets0cocos2d:2BucketsinchThe Map containercocos2d:1ElementinchBuckets1cocos2d:the RandomObjectTag =0cocos2d:before Remove sp0, size of map is2. Cocos2d:after remove Sp0, size of map is1.
5. Best practices
When Cocos2d::map<k, v> () is passed as a parameter, it is declared as a constant reference, such as const cocos2d::map<k, v> () &.
T must be a pointer to a Cocos2d::object subclass object, not another data type or native type
Cocos Basic Tutorial (4) Data structure introduction of cocos2d::map<k,v>