Cocos2d-x 3.0数据结构——cocos2d::Vector
来源:互联网 发布:学术猫数据库 编辑:程序博客网 时间:2024/06/01 07:16
cocos2d::Vector<T>
是一个封装好的能动态增长顺序访问的容器。 cocos2d::Vector<T>
中的元素是按序存取的,它的低层实现数据结构是标准模版库中的标准顺序容器std::vector
。 在Cocos2d-x v3.0 beta之前,使用的是另外一个顺序访问容器cocos2d::CCArray
,不过它将会被废弃。
模版参数
- T - 元素类型
T的类型必须是继承自cocos2d::Object
类型的指针。因为已经将Cocos2d-x的内存管理模型集成到了cocos2d::Vector<T>
中,所以类型参数不能是其他的类型包括基本类型。
内存管理
- 使用现代的c++,本地存储对象比堆存储对象好。所以请不要用new操作来申请
cocos2d::Vector<T>
的堆对象,请使用栈对象。 如果真心想动态分配堆cocos2d::Vector<T>
,请将原始指针用智能指针来覆盖。 cocos2d::Vector<T>
并不是cocos2d::Object的子类,所以不要像使用其他cocos2d类一样来用retain/release和引用计数内存管理。
基本用法
警告:cocos2d::Vector并没有重载[]操作,所以不能直接用下标[i]来获取第i位元素。
cocos2d::Vector提供了不同类型的迭代器,所以我们可以受益于c++的标准函数库,我们可以使用大量标准泛型算法和for_each循环。 除了std::vector容器的操作之外,开发者们还加入许多标准算法诸如:std::find, std::reverse和std::swap,这些算法可以简化很多通用的操作。 要了解更多的api用例,可以参考Cocos2d-x 3.0beta的源码和压缩包里附带的例子。 下面是一些简单的例子:
//create Vector<Sprite*> with default size and add a sprite into itauto sp0 = Sprite::create();sp0->setTag(0);//here we use shared_ptr just as a demo. in your code, please use stack object insteadstd::shared_ptr<Vector<Sprite*>> vec0 = std::make_shared<Vector<Sprite*>>(); //default constructorvec0->pushBack(sp0);//create a Vector<Object*> with a capacity of 5 and add a sprite into itauto sp1 = Sprite::create();sp1->setTag(1);//initialize a vector with a capacityVector<Sprite*> vec1(5);//insert a certain object at a certain indexvec1.insert(0, sp1);//we can also add a whole vectorvec1.pushBack(*vec0);for(auto sp : vec1){ log("sprite tag = %d", sp->getTag());}Vector<Sprite*> vec2(*vec0);if (vec0->equals(vec2)) { //returns true if the two vectors are equal log("pVec0 is equal to pVec2");}if (!vec1.empty()) { //whether the Vector is empty //get the capacity and size of the Vector, noted that the capacity is not necessarily equal to the vector size. if (vec1.capacity() == vec1.size()) { log("pVec1->capacity()==pVec1->size()"); }else{ vec1.shrinkToFit(); //shrinks the vector so the memory footprint corresponds with the number of items log("pVec1->capacity()==%zd; pVec1->size()==%zd",vec1.capacity(),vec1.size()); } //pVec1->swap(0, 1); //swap two elements in Vector by their index vec1.swap(vec1.front(), vec1.back()); //swap two elements in Vector by their value if (vec2.contains(sp0)) { //returns a Boolean value that indicates whether object is present in vector log("The index of sp0 in pVec2 is %zd",vec2.getIndex(sp0)); } //remove the element from the Vector vec1.erase(vec1.find(sp0)); //pVec1->erase(1); //pVec1->eraseObject(sp0,true); //pVec1->popBack(); vec1.clear(); //remove all elements log("The size of pVec1 is %zd",vec1.size());}
输出:
Cocos2d: sprite tag = 1
Cocos2d: sprite tag = 0
Cocos2d: pVec0 is equal to pVec2
Cocos2d: pVec1->capacity()==2; pVec1->size()==2
Cocos2d: The index of sp0 in pVec2 is 0
Cocos2d: The size of pVec1 is 0
最佳用法
- 考虑基于栈的
cocos2d::Vector<T>
优先用于基于堆的。 - 当将
cocos2d::Vector<T>
作为参数传递时,将它声明成常量引用:const cocos2d::Vector<T>&
- 返回值是
cocos2d::Vector<T>
时,直接返回值,这种情况下编译器会优化成移动操作。 - 不要用任何没有继承cocos2d::Object的类型作为
cocos2d::Vector<T>
的数据类型。
0 0
- Cocos2d-x 3.0数据结构——cocos2d::Vector
- cocos2d-x 3.0 数据结构 (1/3) cocos2d::Vector
- Cocos2d-x 3.0数据结构——cocos2d::Map
- Cocos2d-x 3.0数据结构——cocos2d::Value
- Cocos2d-x 3.0 数据结构
- 【amazing cocos2d-x 3.0之七】数据结构Vector
- Cocos2d-x Vector——vector iterators incompatible
- [Cocos2d-x v3.x]cocos2d::Vector
- Cocos2d-x 3.0final -Vector&map&value
- Cocos2d-x 3.0-Vector&map&value
- cocos2d-x开发日志09 —— Vector
- Cocos2d-x 数据结构
- Cocos2d-x 2.0 数据结构
- 22.cocos2d-x数据结构
- cocos2d-x Vector&map&value
- cocos2d-x Vector的使用
- cocos2d-x 3.0 数据结构(2/3) cocos2d::Map
- cocos2d-x 3.0 数据结构(3/3) cocos2d::Value
- Fragment加入到Activity的两种方式
- 重构全局系统架构的方法与工具
- Go语言学习笔记10
- UIView——layout
- 编写一个makefile的简单实例
- Cocos2d-x 3.0数据结构——cocos2d::Vector
- Activity给Fragment传参
- 佳肴 (Standard IO)
- laravel数据迁移
- JAVA 十大算法
- 取数游戏 纪中 1308 蜜汁dp
- Fragment之间传值
- 主线程等待子线程各种方案比较
- poj1185