cocos2d::Vector最佳用法
来源:互联网 发布:诺基亚6120c软件 编辑:程序博客网 时间:2024/05/01 10:09
cocos2d::Vector<T>,下面是官方提供的示例用法:
//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::Vector最佳用法
1.优先考虑用栈生成的Vector<T>,而不是堆。栈生成的性能更好。
2.把Vector<T>做为参数传递时,如果不需要改变它,常把它作为const Vector<T>&,一般对象不需要修改都这么做,可以参考《Effective C++》条款03。
3.返回值是Vector<T>时,直接返回,C++11特性移动对象,编译器将其优化操作,而不是以前的拷贝。不熟悉的靴童可以参考下《C++ Primer 5th》的第十三章的对象移动。
4.Vector<T>,里面的元素必须是继续coco2d::Object。
0 0
- cocos2d::Vector最佳用法
- Cocos2d-x 3.10版本 vector容器用法
- cocos2d::Vector
- C++ Vector And cocos2d Vector
- cocos2d::Vector与std::vector
- vector<vector<int>> 用法
- Vector用法
- vector 用法
- vector用法
- vector 用法
- vector 用法
- Vector用法
- vector用法
- vector用法
- vector用法
- Vector用法
- vector用法
- vector用法
- spoj 375
- Maximum Depth of Binary Tree
- Linux 管理常用命令
- HDU 5040
- hdu 4585 Shaolin 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现
- cocos2d::Vector最佳用法
- 卸载虚拟机后无法上网但能上QQ
- MFC 扩展DLL中使用STL 问题
- visual svnserver 安装失败(服务启动失败)解决方法
- leetcode - Populating Next Right Pointers in Each Node II
- repo使用
- android_定义多个Activity及跳转
- 下载最新的spring框架
- Java内存泄露的理解与解决(转)