>  기사  >  데이터 베이스  >  Cocos2d-x3.0中容器介绍(cocos2d::Vector)

Cocos2d-x3.0中容器介绍(cocos2d::Vector)

WBOY
WBOY원래의
2016-06-07 14:59:041011검색

v3.0加入 定义在”COCOS2DX_ROOT/cocos/base”的”CCVector.h”头文件中。 templateclass CC_DLL Vector; cocos2d::Vector是一个封装好的能动态增长顺序访问的容器。 cocos2d::Vector中的元素是按序存取的,它的低层实现数据结构是标准模版库中的标准顺序容

 

  • v3.0加入

定义在”COCOS2DX_ROOT/cocos/base”的”CCVector.h”头文件中。


templateclass CC_DLL Vector;


cocos2d::Vector是一个封装好的能动态增长顺序访问的容器。

cocos2d::Vector中的元素是按序存取的,它的低层实现数据结构是标准模版库中的标准顺序容器std::vector。

在cocos2d-x v3.0 beta之前,使用的是另外一个顺序访问容器cocos2d::CCArray,不过它将会被废弃。

设计者们将cocos2d::Vector设计为cocos2d::CCArray的替代品,所以建议优先考虑使用cocos2d::Vector。

cocos2d::Vector的一些操作的时间复杂度如下:

  • 随机访问,O(1)
  • 将元素插入到尾部或者删除尾部的元素,O(1)
  • 随机插入或删除, O(n)

模版参数

T – 元素类型

  • T的类型必须是继承自cocos2d::Ref类型的指针。因为已经将cocos2d-x的内存管理模型集成到了cocos2d::Vector中,所以类型参数不能是其他的类型包括基本类型。

内存管理

cocos2d::Vector类只包含一个成员数据:

<span>std</span><span>::</span><span>vector</span><span><span>T</span><span>></span><span> _data</span><span>;</span></span>

_data的内存管理是由编译器自动处理的,如果声明了一个cocos2d::Vector类型,就不必费心去释放内存。

注意:使用现代的c++,本地存储对象比堆存储对象好。所以请不要用new操作来申请cocos2d::Vector的堆对象,请使用栈对象。

如果真心想动态分配堆cocos2d::Vector,请将原始指针用智能指针来覆盖。

警告:cocos2d::Vector并不是cocos2d::Ref的子类,所以不要像使用其他cocos2d类一样来用retain/release和引用计数内存管理。

基本用法

作者们用std::vector的基本操作加上cocos2d-x的内存管理规则来覆盖该模版原先的普通操作。

所以pushBack()操作将会保留传递过来的参数,而popBack()则会释放掉容器中最后的一个元素。

当你使用这些操作的时候,你需要特别注意这些受托管的对象,对于新手来说,这往往是陷阱。

警告:cocos2d::Vector并没有重载[]操作,所以不能直接用下标[i]来获取第i位元素。

cocos2d::Vector提供了不同类型的迭代器,所以我们可以受益于c++的标准函数库,我们可以使用大量标准泛型算法和for_each循环。

除了std::vector容器的操作之外,开发者们还加入许多标准算法诸如:std::find, std::reverse和std::swap,这些算法可以简化很多通用的操作。

要了解更多的api用例,可以参考cocos2d-x 3.0的源码和压缩包里附带的例子。

下面是一些简单的例子:

<span>//create Vector<sprite> with default size and add a sprite into it</sprite></span><span>auto</span><span> sp0 </span><span>=</span><span>Sprite</span><span>::</span><span>create</span><span>();</span><span>
sp0</span><span>-></span><span>setTag</span><span>(</span><span>0</span><span>);</span><span>//here we use shared_ptr just as a demo. in your code, please use stack object instead</span><span>
std</span><span>::</span><span>shared_ptr</span><span><span>Vector</span><span><span>Sprite</span><span>*>></span><span>  vec0 </span><span>=</span><span> std</span><span>::</span><span>make_shared</span><span><span>Vector</span><span><span>Sprite</span><span>*>>();</span><span>//default constructor</span><span>
vec0</span><span>-></span><span>pushBack</span><span>(</span><span>sp0</span><span>);</span><span>//create a Vector<ref> with a capacity of 5 and add a sprite into it</ref></span><span>auto</span><span> sp1 </span><span>=</span><span>Sprite</span><span>::</span><span>create</span><span>();</span><span>
sp1</span><span>-></span><span>setTag</span><span>(</span><span>1</span><span>);</span><span>//initialize a vector with a capacity</span><span>Vector</span><span><span>Sprite</span><span>*></span><span>  vec1</span><span>(</span><span>5</span><span>);</span><span>//insert a certain object at a certain index</span><span>
vec1</span><span>.</span><span>insert</span><span>(</span><span>0</span><span>,</span><span> sp1</span><span>);</span><span>//we can also add a whole vector</span><span>
vec1</span><span>.</span><span>pushBack</span><span>(*</span><span>vec0</span><span>);</span><span>for</span><span>(</span><span>auto</span><span> sp </span><span>:</span><span> vec1</span><span>)</span><span>{</span><span>
    log</span><span>(</span><span>"sprite tag = %d"</span><span>,</span><span> sp</span><span>-></span><span>getTag</span><span>());</span><span>}</span><span>Vector</span><span><span>Sprite</span><span>*></span><span> vec2</span><span>(*</span><span>vec0</span><span>);</span><span>if</span><span>(</span><span>vec0</span><span>-></span><span>equals</span><span>(</span><span>vec2</span><span>))</span><span>{</span><span>//returns true if the two vectors are equal</span><span>
    log</span><span>(</span><span>"pVec0 is equal to pVec2"</span><span>);</span><span>}</span><span>if</span><span>(!</span><span>vec1</span><span>.</span><span>empty</span><span>())</span><span>{</span><span>//whether the Vector is empty</span><span>//get the capacity and size of the Vector, noted that the capacity is not necessarily equal to the vector size.</span><span>if</span><span>(</span><span>vec1</span><span>.</span><span>capacity</span><span>()</span><span>==</span><span> vec1</span><span>.</span><span>size</span><span>())</span><span>{</span><span>
        log</span><span>(</span><span>"pVec1->capacity()==pVec1->size()"</span><span>);</span><span>}</span><span>else</span><span>{</span><span>
        vec1</span><span>.</span><span>shrinkToFit</span><span>();</span><span>//shrinks the vector so the memory footprint corresponds with the number of items</span><span>
        log</span><span>(</span><span>"pVec1->capacity()==%zd; pVec1->size()==%zd"</span><span>,</span><span>vec1</span><span>.</span><span>capacity</span><span>(),</span><span>vec1</span><span>.</span><span>size</span><span>());</span><span>}</span><span>//pVec1->swap(0, 1);  //swap two elements in Vector by their index</span><span>
    vec1</span><span>.</span><span>swap</span><span>(</span><span>vec1</span><span>.</span><span>front</span><span>(),</span><span> vec1</span><span>.</span><span>back</span><span>());</span><span>//swap two elements in Vector by their value</span><span>if</span><span>(</span><span>vec2</span><span>.</span><span>contains</span><span>(</span><span>sp0</span><span>))</span><span>{</span><span>//returns a Boolean value that indicates whether object is present in vector</span><span>
        log</span><span>(</span><span>"The index of sp0 in pVec2 is %zd"</span><span>,</span><span>vec2</span><span>.</span><span>getIndex</span><span>(</span><span>sp0</span><span>));</span><span>}</span><span>//remove the element from the Vector</span><span>
    vec1</span><span>.</span><span>erase</span><span>(</span><span>vec1</span><span>.</span><span>find</span><span>(</span><span>sp0</span><span>));</span><span>//pVec1->erase(1);</span><span>//pVec1->eraseObject(sp0,true);</span><span>//pVec1->popBack();</span><span>

vec1</span><span>.</span><span>clear</span><span>();</span><span>//remove all elements</span><span>
log</span><span>(</span><span>"The size of pVec1 is %zd"</span><span>,</span><span>vec1</span><span>.</span><span>size</span><span>());</span><span>}</span></span></span></span></span></span></span>

输出:

<span>Cocos2d</span><span>:</span><span> sprite tag </span><span>=</span><span>1</span><span>Cocos2d</span><span>:</span><span> sprite tag </span><span>=</span><span>0</span><span>Cocos2d</span><span>:</span><span> pVec0 </span><span>is</span><span> equal to pVec2
</span><span>Cocos2d</span><span>:</span><span> pVec1</span><span>-></span><span>capacity</span><span>()==</span><span>2</span><span>;</span><span> pVec1</span><span>-></span><span>size</span><span>()==</span><span>2</span><span>Cocos2d</span><span>:</span><span>The</span><span> index of sp0 </span><span>in</span><span> pVec2 </span><span>is</span><span>0</span><span>Cocos2d</span><span>:</span><span>The</span><span> size of pVec1 </span><span>is</span><span>0</span>

推荐做法

  • 考虑基于栈的cocos2d::Vector优先用于基于堆的
  • 当将cocos2d::Vector作为参数传递时,将它声明成常量引用:const cocos2d::Vector&
  • 返回值是cocos2d::Vector时,直接返回值,这种情况下编译器会优化成移动操作。
  • 不要用任何没有继承cocos2d::Ref的类型作为cocos2d::Vector的数据类型。
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.