c_cpp 关于数组使用的一切。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 关于数组使用的一切。相关的知识,希望对你有一定的参考价值。

// returns true if the two vectors are equal
Vector<Sprite*> vec2(*vec0);
if (vec0->equals(vec2)) { ... }

// whether the Vector is empty
if(!vec1.empty()) { ... }

// shrinks the vector so the memory footprint corresponds with the number of items
vec1.shrinkToFit();

// checks whether an object is in the container. 
vec1.contains(sprite0);

// returns the number of elements in the Vector
vec1.size()

// the size of the currently allocated storage capacity in the Vector, measured in terms of the number elements it can hold.
// this capacity is not necessarily equal to the Vector size. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion. 
vec1.capacity()
for (auto bullet: this->_bullets)
{
    if (bullet->getPositionX() > 160)
    {
        //  ...
    }
}
// add an object
auto sprite1 = Sprite::create();
this->vec1.pushBack(sprite1);

// insert a certain object at a certain index
auto sprite1 = Sprite::create();
vec1.insert(0, sprite1);

// we can also add a whole vector
vec1.pushBack(*vec0);

// remove the element from the Vector
this->vec1.eraseObject(bullet);
//vec1.erase(vec1.find(sp0));
//pVec1->erase(1);
//pVec1->eraseObject(sp0,true);
//pVec1->popBack();

// remove all elements
vec1.clear();
// declare and Init Array
Vector<Touch *> _touches;

// Attention!!! : Vector can hold pointers that is Ref or subclass of Ref (e.g.:Node) only, 
// so for example Vector< Node* > is valid, 
// but not Vector< int > or Vector< Node >, otherwise it will fail to compile.

[Blog](http://dev.bunnyhero.org/2014/01/cocos2d-x-30-beta-the-new-vector-class/)

[Wiki](http://www.cocos2d-x.org/wiki/Vector%3CT%3E)

Vector<Sprite *> allSoldiers;

// initialize a vector with a capacity
Vector<Sprite *>  vec1(5);


HelloWorld::HelloWorld() : _bullets(100)
{
}
// or

this->_bullets = cocos2d::Vector<cocos2d::Sprite *>{100};

以上是关于c_cpp 关于数组使用的一切。的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 使用数组实现堆栈

c_cpp 使用数组实现循环队列

c_cpp 使用数组实现线性队列

c_cpp 使用类和数组实现堆栈

c_cpp 该C程序使用指针计算数组元素的总和。程序使用指针遍历数组并将元素加起来

c_cpp 在cpp中使用char数组