文章转载于:http://www.cocoachina.com/gamedev/cocos/2014/0604/8666.html
v3.0 beta加入
template<class T>class CC_DLL Vector;
std::vector<T> _data;
//create Vector<Sprite*> with default size and add a sprite into it auto sp0 = Sprite::create(); sp0->setTag(0); //here we use shared_ptr just as a demo. in your code, please use stack object instead std::shared_ptr<Vector<Sprite*>> vec0 = std::make_shared<Vector<Sprite*>>(); //default constructor vec0->pushBack(sp0); //create a Vector<Object*> with a capacity of 5 and add a sprite into it auto sp1 = Sprite::create(); sp1->setTag(1); //initialize a vector with a capacity Vector<Sprite*> vec1(5); //insert a certain object at a certain index vec1.insert(0, sp1); //we can also add a whole vector vec1.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