Cocos2dx template container is simple to use (Vector, Map, Value)

Source: Internet
Author: User
Prior to cocos2dxv3.0beta, there were sequential containers cocos2d: CCArray, and cocos2d: CCDictionary. However, after the new version, both containers will be replaced by cocos2d: VectorT and cocos2d: MAPKs and V. 1. cocos2d: VectorTcocos2d: VectorT is an ordered container that encapsulates an array of dynamic sizes. Its element is

Prior to cocos2dxv3.0beta, there were sequential containers cocos2d: CCArray, and cocos2d: CCDictionary. However, after the new version, both containers will be replaced by cocos2d: VectorT and cocos2d: MAPKs and V. 1. cocos2d: VectorT cocos2d: VectorT is an ordered container that encapsulates an array of dynamic sizes. Its element is

Prior to cocos2dxv3.0beta, there were sequential containers cocos2d: CCArray, and cocos2d: CCDictionary. However, after the new version, both containers will be cocos2d: Vector And cocos2d: Map .

1. cocos2d: Vector

Cocos2d: Vector Is an ordered container that encapsulates an array of dynamic sizes.
Its elements are continuously stored, cocos2d: Vector Is automatically processed. Its internal data structure implementation is actually the STL standard sequence container std: vector.

Remember that T must be a pointer to a cocos2d: Object subclass Object. It cannot be another data type or native type, because the Cocos2d-x's memory management model has been integrated into cocos2d: Vector Starting from v3.0 beta ). You don't have to worry about memory management when using Vector. The class has already been done for you.

Easy to use:

// Create a Vector with the default size And then add an genie to it.

Auto sp0 = Sprite: create ();

Vec0-> pushBack (sp0 );

// Initialize a vector with a capacity

Auto sp1 = Sprite: create ();

Vector Vec1 (5 );

Vec1.insert (0, sp1 );

This container is equivalent to a variable-size container used to save the cocos2d: object pointer object. To save the basic type, you can use std: vector or the simplest array. For more information, see the http://www.cocoachina.com/bbs/read.php? Tid = 199793

2. cocos2d: Map

Cocos2d: Map Is an associated container template that uses std: unordered_map internally.
Std: unordered_map is an associated container consisting of key-value pairs. It allows you to quickly search individual elements based on keys.

In unordered_map, key value is generally used to identify a unique element, while mapped value is an object, and its content is associated with the key value. Internally, the unordered_map elements do not depend on the key or mapped value to sort in any specific way, but depend on their hash values, this allows you to quickly access a single element using their key values (using average time complexity ).

Each element of the K-key value type unordered_map is uniquely identified by its key value.

The V-mapped value type. T must be a pointer to a cocos2d: Object subclass Object. It cannot be another data type or native type, because we have integrated the Cocos2d-x's memory management model into cocos2d: Map .

The value of K can be of another type, but the value of V must be a pointer to a cocos2d: Object subclass Object.

Easy to use

// Create a Map with the default size And then add an genie to it.

Auto sp0 = Sprite: create ();

Sp0-> setTag (0 );

Map Map0;

Std: string mapKey0 = "MAP_KEY_0 ";

Map0.insert (mapKey0, sp0 );

Map Is a key-value container. To save the value of the basic type, you can use std: map Or std: unordered_map . See http://www.cocoachina.com/bbs/read.php for details? Tid = 199916

3. cocos2d: Value

Cocos2d: Value is a native type that contains many types (int, float, double, bool, unsigned char, char *, and std: string) plus std: vector , Std: unordered_map And std: unordered_map .
You can put all the native types mentioned above into the cocos2d: Value object, convert them to the corresponding native type, and vice versa.
Some native encapsulation classes such as CCBool, CCFloat, CCDouble, and CCinteger exist before Cocos2d-x v3.0 beta, which will be discarded.
Note: When processing native types and containers, use cocos2d: Vector , Cocos2d: Map And cocos2d: Value.

The Value does not manage the memory with the Vector or Map itself. cocos2d: the memory of the Value is automatically processed by its own destructor. Therefore, when processing the cocos2d: Value memory, stick to the best practices with c ++ memory management rules.

Easy to use

Value val1 (65); // initialize with an int

// Value val1 (3.4f); // use a float for initialization

// Value val1 (3.5); // initialize with a double

Log ("val1.asByte () = % d", val1.asInt (); // read int data

The Cocos2d: Value class is not a subclass of cocos2d: Object, so cocos2d: Vector or cocos2d: Map cannot be used. It is only one cocos that can contain other numeric types. For details, see Http://www.cocoachina.com/bbs/read.php? Tid = 200034

These are modified based on others, just for your own favorites.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.