Cocos2d::vector
cocos2d::Vector<T>
is a well-encapsulated container that dynamically grows sequential access. The cocos2d::Vector<T>
elements in the sequence are accessed sequentially, and its lower-level implementation data structures are standard sequential containers in the standard Template Library std::vector
.
T -element type
- The type of t must be a pointer that inherits from
cocos2d::Object
the type. Because the COCOS2D-X memory management model has been integrated into cocos2d::Vector<T>
, the type parameter cannot be other types including the base type.
Note : Using modern C + +, local storage objects are better than heap storage objects. So please do not use the new operation to request cocos2d::Vector<T>
the heap object, using the Stack object.
warning : cocos2d::Vector<T>
not cocos2d::Object
a subclass, so do not use Retain/release and reference count memory management as with other cocos2d classes.
usage : The pushback () operation will retain the passed arguments, and Popback () will release the last element in the container. The cocos2d::Vector<T>
[] operation is not overloaded, so the subscript [i] cannot be used directly to obtain the I-bit element. cocos2d::Vector<T>
different types of iterators are available, and we can use a number of standard generic algorithms and For_each loops. If you have enough reason to dynamically allocate cocos2d::vector<t> on the heap, replace the original pointer with a smart pointer, such as SHARED_PTR,UNIQUE_PTR.
A simple example is provided here:
//Create a VECTOR<SPRITE*> with the default size, and then add a sprite to itAuto SP0 =sprite::create (); Sp0->settag (0);//Here we use the demo shared_ptr, in your code, use the Stack object insteadstd::shared_ptr<vector<sprite*>> vec0 = std::make_shared<vector<sprite*>> ();//default constructorVec0->pushback (SP0);//use capacity to create a vector<object*> for 5来, and then add a sprite to itAuto SP1 =sprite::create (); SP1->settag (1); //initialize a vector with one capacityVector<sprite*> VEC1 (5);//Insert a definite object in a definite positionVec1.insert (0, SP1); //we can also join a whole vectorVec1.pushback (*vec0); for(auto sp:vec1) {log ("Sprite tag =%d", sp->Gettag ());} Vector<Sprite*> VEC2 (*vec0);if(Vec0->equals (VEC2)) {//If two vectors are the same, return to TrueLog"pVec0 is equal to PVEC2");}if(!vec1.empty ()) {//determine if the vector is empty//to get the capacity and size of a vector, note that capacity is not necessarily equal to size if(vec1.capacity () = =vec1.size ()) {Log ("pvec1->capacity () ==pvec1->size ( )"); }Else{vec1.shrinktofit (); //shrink the vector so that the number of elements on the memory correspondsLog"pvec1->capacity () ==%zd; Pvec1->size () ==%zd", Vec1.capacity (), vec1.size ()); } //pvec1->swap (0, 1); //Exchange two elements in a vector by indexVec1.swap (Vec1.front (), Vec1.back ());//Exchange two elements in a vector by value if(Vec2.contains (SP0)) {//returns a Boolean value that indicates whether the object exists in the vectorLog"The index of SP0 in PVEC2 is%zd", Vec2.getindex (SP0)); } //removing elements from a vectorvec1.erase (Vec1.find (SP0)); //pvec1->erase (1); //Pvec1->eraseobject (sp0,true); //Pvec1->popback ();vec1.clear ();//Remove all elementsLog"The size of PVEC1 is%zd", Vec1.size ());}
Output results
10cocos2d:pvec0 is equal to PVEC2COCOS2D:PVEC1->capacity () = =2; Pvec1->size () = =2in00
Best practices
- Prefer to use stack-based cocos2d::vector<t> without using heap-based cocos2d::vector<t>.
- When Cocos2d::vector<t> is passed as a parameter, it is declared as a constant reference, such as a const cocos2d::vector<t>&.
- When a cocos2d::vector<t> is returned from a function, the value object is simply returned. The compiler uses the move semantics to optimize this situation.
- Do not attempt to save any data type objects other than the Cocos2d::object subclass object pointer in cocos2d::vector<t>.
Cocos Basic Tutorial (4) Data structure introduction of Cocos2d::vector